代码之家  ›  专栏  ›  技术社区  ›  Alex Gordon

返回odata查询的计数

  •  1
  • Alex Gordon  · 技术社区  · 9 年前

    如果我有以下问题:

    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. ?

    2 回复  |  直到 9 年前
        1
  •  3
  •   Aron    9 年前

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

    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>
    

    我还应该提到,没有必要返回所有字段,因此我们可以使用&$选择=帐户ID。

    你可以得到@odata。使用关联引用计数: 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

        2
  •  1
  •   Maximus    8 年前

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