首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CRM中从OData查询中获取选项集文本

在CRM中从OData查询中获取选项集文本
EN

Stack Overflow用户
提问于 2017-11-09 09:24:49
回答 3查看 7.2K关注 0票数 4
代码语言:javascript
复制
https://example.com/crm/api/data/v8.2/accounts?$select=custom_optionset

上面的查询选择CRM中选项集字段中的所有值。返回数据如下所示:

代码语言:javascript
复制
{
    {
        "@odata.etag":"W/\"112607639\"","custom_optionset":285960000,"accountid":"a08f0bd1-e2c4-e111-8c9a-00155d0aa573"
    },
    {
        "@odata.etag":"W/\"112615384\"","custom_optionset":285960010,"accountid":"a18f0bd1-e2c4-e111-8c9a-00155d0aa573"
    }
}

我不想要选项集的价值。我要相关的文本标签。我怎么弄到这个?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-11-09 12:00:28

若要使用webapi获取选项集文本,请在请求头中使用下面的代码段。

代码语言:javascript
复制
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");

这将返回类似于查找FormattedValue的选择列表文本。

整个代码示例:

代码语言:javascript
复制
function retrieveEntity(entityName, Id, columnSet) {
    var serverURL = Xrm.Page.context.getClientUrl();
    var Query = entityName + "(" + Id + ")" + columnSet;
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.2/" + Query, true);
    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 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data != null {
                        alert(data["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"]); //for lookup text
                        alert(data["paymenttermscode@OData.Community.Display.V1.FormattedValue"]); //for optionset text
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        req.send();
    }

参考文献

票数 14
EN

Stack Overflow用户

发布于 2017-11-09 12:02:16

如果使用Jason的CRM RESTBuilder解决方案,它将创建一个类似于此的查询,其中包括标题req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

代码语言:javascript
复制
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(674D7FDC-47AE-E711-8108-5065F38A3BA1)?$select=accountid,industrycode", 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 result = JSON.parse(this.response);
            var accountid = result["accountid"];
            var industrycode = result["industrycode"];
            var industrycode_formatted = result["industrycode@OData.Community.Display.V1.FormattedValue"];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

结果包括选项集标签:

代码语言:javascript
复制
{
@odata.context:"https://myorg.crm.dynamics.com/api/data/v8.2/$metadata#accounts(accountid,industrycode)/$entity",
@odata.etag:"W/"1959756"",
accountid:"674d7fdc-47ae-e711-8108-5065f38a3ba1",
industrycode@OData.Community.Display.V1.FormattedValue:"Accounting",
industrycode:1
}
票数 3
EN

Stack Overflow用户

发布于 2017-11-10 08:09:02

我发现使用XrmQuery在客户关系管理中调用Web是最容易的(完全公开:我为开发这一工具的公司工作)。不需要手动修改标头和XMLHttpRequests,而且您可以获得对您自己的CRM中的实体和字段的完全强类型支持。

例如,检索特定帐户并选择industrycode字段如下所示:

代码语言:javascript
复制
    XrmQuery.retrieve(a => a.accounts, "5B86C6EB-DE6D-E611-80DF-C4346BADF080")
        .select(a => [a.industrycode])
        .includeFormattedValues()
        .execute(a => {
            console.log("Industry is: " + a.industrycode_formatted);
        });

自动添加.includeFormattedValues()使您可以访问一个简单称为industrycode_formatted的字段。

对于具有下列选项集值的帐户:

以上代码将返回:

行业:广播印刷出版

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47198130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档