javascript - 计算 Dynamics crm 中查找字段的长度

标签 javascript dynamics-crm crm fetchxml

我创建了一个 fetchxml 来过滤 CRM 中的契约(Contract)查找字段。我希望能够查看契约(Contract)查找字段中有多少条记录。如果只有一条记录,我希望能够自动填充该查找字段。

尝试使用 API 函数来循环 fetchxml 记录,但没有返回计数。还有其他方法可以获得我需要的结果吗?

 function buildCustomView() {
        // Some GUID but only needs to be unique among the other available views for the lookup
        var viewId = "{00000000-0000-0000-0000-000000000001}";
        var viewDisplayName = "Contracts for This Account";

        //Only need Contracts for the selected Worksite and buisness unit if filled in
        var worksite = getFieldValue("hc_worksite");

        if (!worksite) { return; }
        if (!worksite[0]) { return; }
        if (!worksite[0].id) { return; }
        //check if buisness unit is availble and if it is can filter lookup further
        var buisnessUnit = getFieldValue("hc_businessunit");
        var condition;
        if (buisnessUnit != null) {
            condition = "<filter type='and'>" +
                                  "<condition attribute='hc_businessunit' operator='eq' value='" + buisnessUnit[0].id + "' />" +
                                "</filter>";
            ///if take out the account name, contract lookup will still be filteredl
        } else {
            condition = "";
        }

        //Get all Contracts that pertain to the selected worksite
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'  >" +
                         "<entity name='contract'>" +
                            "<attribute name='title' />" +
                            "<attribute name='customerid' />" +
                            "<attribute name='contractid' />" +
                            "<attribute name='hc_contracttype' />" +
                            "<attribute name='hc_contractsubtype' />" +
                            "<attribute name='activeon' />" +
                            "<attribute name='hc_contractlevel' />" +
                            "<attribute name='expireson' />" +
                            "<attribute name='hc_contractaccount' />" +
                            "<attribute name='hc_contract' />" +
                            "<order attribute='title' descending='false' />" + condition +
                            "<link-entity name='hc_account_contract' from='contractid' to='contractid' visible='false' intersect='true'>" +
                              "<link-entity name='account' from='accountid' to='accountid' alias='ab'>" +
                                "<filter type='and'>" +
                                  "<condition attribute='accountid' operator='eq' value='" + worksite[0].id + "' />" +
                                "</filter>" +
                              "</link-entity>" +
                            "</link-entity>" +
                          "</entity>" +
                        "</fetch>";
        //Set up the whole view's UI
        var layoutXml = "<grid name='resultset' object='1' jump='hc_contract' select='1' icon='1' preview='2'>" +
                         "<row name='result' id='contractid'>" +
                          "<cell name='title' width='300' />" +
                          "<cell name='hc_contractaccount' width='200' />" +
                          "<cell name='hc_contracttype' width='100' />" +
                          "<cell name='hc_contractsubtype' width='100' />" +
                          "<cell name='hc_contractlevel' width='100' />" +
                          "<cell name='activeon' width='100' />" +
                          "<cell name='expireson' width='100' />" +
                          "<cell name='customerid' width='150' />" +
                          "<cell name='hc_contract' width='100' />" +
                         "</row>" +
                        "</grid>";

        try {
            //Set the view
            Xrm.Page.getControl("hc_contract").addCustomView(viewId, "contract", viewDisplayName, fetchXml, layoutXml, true);
        } catch (e) {
            Xrm.Utility.alertDialog("Error: " + e.message);
        }
        var count = 0;
        //count results of fetch xml
         SDK.WEBAPI.executeFetchSync("contract", fetchXml,
            function (results) {
                
               for (i=0;i<results.length; i++){
                    count++;
                }
                Xrm.Utility.alertDialog(count);
            },
            function (error) {
                //Show error
                alertDialog("Error: " + error.message);
            });
        
    }

最佳答案

您可以使用计数聚合属性对 FetchXML 查询返回的记录进行计数。请参阅documentation here .

aggregate='true' 放入提取的第一行:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true'>

然后添加计数聚合属性:

<attribute name='contractid' alias='contract_count' aggregate='count' />

然后获取 executeFetchSync 成功回调中的值:

results.entities[0].contract_count;

关于javascript - 计算 Dynamics crm 中查找字段的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416668/

相关文章:

javascript - React Router 不会改变组件

javascript - 在 dojo 中单击按钮时启用复选框

javascript - 如何在初始化 ServiceWorker 时声明客户端以避免重新加载页面?

javascript - 尝试在 CRM 2011 UR 12 中使用 XRMServiceToolkit 反序列化消息时出错

deployment - 如何在 Dynamics CRM 2011 中导出/导入业务单位和安全角色?

c# - CRM 2011 Pre-Operation Update 相关实体查找字段

php - 如何登录网站或访问现有 cookie 以使用 file_get_contents

javascript - Xrm.Navigation.openForm 不尊重 formid

c# - 如何使用 web api 获取资源的可用性?

java - 使用 Salesforce CRUD 元数据 API 创建自定义对象时出现异常