【问题标题】:Returning count for an odata query返回 odata 查询的计数
【发布时间】:2017-07-11 15:39:34
【问题描述】:

如果我有如下查询:

http://myCRMOrg/api/data/v8.1/accounts?$filter=_chr_accountstatusid_value%20eq%2079A024B5-3D7C-E211-8B29-00155DC86B6F%20and%20accountid%20eq%20e929baaf-483b-e711-9425-00155dc0d345&$count=true

请注意我指定的是$count=true

它会返回:

{
  "@odata.context":"http://myCRMORG/api/data/v8.1/$metadata#accounts","@odata.count":1,"value":[
    {
      "@odata.etag":"W/\"1812635\"","
      //100 fields and lots of data
    }
  ]
}

我们如何重构这个查询以简单地返回 1

【问题讨论】:

    标签: odata dynamics-crm crm dynamics-crm-2016 dynamics-crm-webapi


    【解决方案1】:

    我不确定我是否正确理解了您的问题,因为如果您按 AccountId 过滤帐户,您只会得到 0 或 1 个结果。所以,如果你得到一个结果,你就知道计数是 1。

    无论如何,要获得计数,您可以像这样使用适当的 FetchXml 聚合:

    https://xedev29.api.crm.dynamics.com/api/data/v8.2/accounts?fetchXml=
    <fetch aggregate='true'>
        <entity name='account'>
            <attribute name='accountid' aggregate='count' alias='Count' />
        </entity>
    </fetch>
    

    返回:

    {
      "@odata.context":"https://xedev29.api.crm.dynamics.com/api/data/v8.2/$metadata#accounts","value":[
        {
          "Count":337
        }
      ]
    }
    

    并随时将您的过滤器添加到 FetchXml:

    https://xedev29.api.crm.dynamics.com/api/data/v8.2/accounts?fetchXml=
    <fetch aggregate='true'>
        <entity name='account'>
        <attribute name='accountid' aggregate='count' alias='Count' />
            <filter type='and' >
                <condition attribute='chr_accountstatusid' operator='eq' value='D1C4CD52-1E51-E711-8122-6C3BE5B3B698'/>
                <condition attribute='statecode' operator='eq' value='0' />
            </filter>
        </entity>
    </fetch>
    

    当然,您会受到通常的 FetchXml 聚合限制(即最高计数为 50K 行)。尽管如果您需要,我已经找到了解决此问题的方法。

    我还应该提到,不需要返回所有字段,所以我们可以使用 &$select=accountid。

    您可以通过关联引用获取@odata.count:result["@odata.count"]

    这是一个完整的例子:

    function count(){
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=accountid&$filter=accountid%20eq%20D1C4CD52-1E51-E711-8122-6C3BE5B3B698&$count=true", 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);
                    console.log(result["@odata.count"]);
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }
    

    此外,我应该提一下,如果您正在使用 WebAPI 进行大量工作,Jason Lattimer's CRMRESTBuilder 非常方便。

    您可能还想查看David Yack's WebAPI helper

    【讨论】:

    • 好吧,我只是想确定知道结果集是否为空的最快方法
    • 好的,那么发布你的 onSuccess 回调代码可能会有所帮助......我想你会想要检查 results == null 或 results[0] == null。
    【解决方案2】:

    我有同样的问题。我所做的是我在过滤器之前添加了计数并且它起作用了。示例:

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts/$count/?$select=accountid&$filter=accountid%20eq%20D1C4CD52-1E51-E711-8122-6C3BE5B3B698&", true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多