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

时间:2019-04-01 17:33:53

标签: javascript dynamics-crm crm

我正在使用http请求获取查找字段的值,但无法将其格式化为result["_log_postalcode_value"]-显示为“ ff4c33be-5139-e211-bd26-e41f13be0af4”,但是result["_log_postalcode_value@OData.Community.Display.V1.FormattedValue"] -未定义。我努力找出代码中的错误,也尝试使用Google搜索,但没有任何答案。也许有人可以帮助我。

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["_log_postalcode_value@OData.Community.Display.V1.FormattedValue"]); // shows undefined

1 个答案:

答案 0 :(得分:0)

您能否尝试“ CRM Rest”构建器,并查看收到的输出。 我只是在我的一个跟踪实例中尝试了您的用例,它对我有用

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"

}