来自Microsoft Dynamics CRM中的Web API的查找字段格式化值未定义(无法获取格式化值)

问题描述 投票:1回答:1

我正在使用http请求来获取查找字段的值,而我无法将其格式化为result["_log_postalcode_value"] - 显示“ff4c33be-5139-e211-bd26-e41f13be0af4”,但result["[email protected]"] - 未定义。我努力找出我的代码中的错误,也尝试谷歌但没有答案。也许有人可以帮助我。

var req = new XMLHttpRequest();

req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + guid + ")?$select=_log_postalcode_value", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue"); 
req.onreadystatechange = function () {
    if (this.readyState == 4) {          
        req.onreadystatechange = null;   
        if (this.status == 200) {
            var result = JSON.parse(this.response);             
            console.log(result["_log_postalcode_value"]);//shows "ff4c33be-5139-e211-bd26-e41f13be0af4"
            console.log(result["[email protected]"]); // shows undefined
javascript dynamics-crm crm
1个回答
0
投票

您可以尝试CRM Rest构建器并查看您收到的输出。我只是在我的一个Trail实例中尝试过你的用例,它对我有用

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(DC86C293-CA4F-E911-A82F-000D3A385A1C)?$select=accountid,_crmp_languagecode_value", false);
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 result = JSON.parse(this.response);
            var accountid = result["accountid"];
            var _crmp_languagecode_value = result["_crmp_languagecode_value"];
            var _crmp_languagecode_value_formatted = result["_crmp_languagecode_value@OData.Community.Display.V1.FormattedValue"];
            var _crmp_languagecode_value_lookuplogicalname = result["_crmp_languagecode_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

这是我收到的输出

{

    @odata.context:"[ORGURL]/api/data/v8.2/$metadata#accounts(accountid,_crmp_languagecode_value)/$entity",
    @odata.etag:"W/"3139254"",
    accountid:"dc86c293-ca4f-e911-a82f-000d3a385a1c",
    _crmp_languagecode_value@OData.Community.Display.V1.FormattedValue:"FR",
    _crmp_languagecode_value@Microsoft.Dynamics.CRM.associatednavigationproperty:"crmp_languagecode",
    _crmp_languagecode_value@Microsoft.Dynamics.CRM.lookuplogicalname:"crmp_languagecode",
    _crmp_languagecode_value:"78a6bb04-88db-e811-a961-000d3ab42b3b"

}
© www.soinside.com 2019 - 2024. All rights reserved.