我一直在研究这段代码。当我运行它并且某些字段不存在时,会弹出function customerSelected() {
var customerID = Xrm.Page.getAttribute("customerid").getValue();
var custID = customerID[0].id.substr(1, 36);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=_defaultpricelevelid_value,paymenttermscode,shippingmethodcode&$filter=accountid eq " + custID + "&$orderby=name asc", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var _defaultpricelevelid_value = results.value[i]["_defaultpricelevelid_value"];
var _defaultpricelevelid_value_formatted = results.value[i]["_defaultpricelevelid_value@OData.Community.Display.V1.FormattedValue"];
var _defaultpricelevelid_value_lookuplogicalname = results.value[i]["_defaultpricelevelid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
var paymenttermscode = results.value[i]["paymenttermscode"];
var paymenttermscode_formatted = results.value[i]["paymenttermscode@OData.Community.Display.V1.FormattedValue"];
var shippingmethodcode = results.value[i]["shippingmethodcode"];
var shippingmethodcode_formatted = results.value[i]["shippingmethodcode@OData.Community.Display.V1.FormattedValue"];
var _defaultpricelevelid_lookupVal = new Array();
_defaultpricelevelid_lookupVal[0] = new Object();
_defaultpricelevelid_lookupVal[0].id = _defaultpricelevelid_value;
_defaultpricelevelid_lookupVal[0].name = _defaultpricelevelid_value_formatted;
_defaultpricelevelid_lookupVal[0].entityType = _defaultpricelevelid_value_lookuplogicalname;
if ("pricelevelid" != null)
{
Xrm.Page.getAttribute("pricelevelid").setValue(_defaultpricelevelid_lookupVal);
}
if ("paymenttermscode" != null)
{
Xrm.Page.getAttribute("paymenttermscode").setValue(_defaultpricelevelid_lookupVal);
}
if ("shippingmethodcode" != null)
{
Xrm.Page.getAttribute("shippingmethodcode").setValue(_defaultpricelevelid_lookupVal);
}
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
};
的错误。我试过If语句,但它仍然无法正常工作。我读过它可能与货币有关,但我似乎无法弄清楚是什么。我是初学者,大部分都不是我的代码。救命!感谢。
{{1}}
答案 0 :(得分:1)
您的查询将始终返回一个记录,因此使用循环迭代结果没有意义。
条件("pricelevelid" != null)
始终为true
。它应该是(_defaultpricelevelid_value != null)
。下面的行也是如此。
您的代码将_defaultpricelevelid_lookupVal
分配给三个单独的属性,这些属性可能不正确。显然,该对象应该表示引用实体pricelevel
的查找ID值。因此,将它分配给属性pricelevelid
才有意义。 paymenttermscode
和shippingmethodcode
是选项集属性,显然需要填充同名变量的值。
使用以下代码替换for
循环:
if (results.value[0]._defaultpricelevelid_value != null) {
var priceLevelId = [{
id = results.value[0]._defaultpricelevelid_value,
name = results.value[0]["_defaultpricelevelid_value@OData.Community.Display.V1.FormattedValue"],
entityType = "pricelevel"
}];
Xrm.Page.getAttribute("pricelevelid").setValue(priceLevelId);
}
if (results.value[0].paymenttermscode != null) {
Xrm.Page.getAttribute("paymenttermscode").setValue(results.value[0].paymenttermscode);
}
if (results.value[0].shippingmethodcode != null) {
Xrm.Page.getAttribute("shippingmethodcode").setValue(results.value[0].shippingmethodcode);
}