使用CRM REST Builder的Predefined Query在js结合FetchXML语句进行查询

发布时间 2023-07-10 15:14:46作者: 流浪阿丁

一般情况下使用拓展工具RESTBuilder编辑器,可以很方便的进行操作js中增删改查均能实现,但在某些较为特殊的场景下,需要根据条件去拼接查询过滤条件的,使用编辑器生成的代码无法实现,需要结合使用fetchXML,比如某个条件多个值都查询需要使用in查询,再或者需要过滤关联表中的某个字段的值。

代码使用示例:

 1 /**
 2  * 根据出口目的国以及限售区域判断是否存在不可销售的产品,如果存在,返回false
 3  */
 4 function checkProductRegion() {
 5     var isSubmit = true;
 6     let returnAnalysisId = commonUtil.delBrackets(Xrm.Page.data.entity.getId());
 7     let destcountryId = commonUtil.getLookupId("new_destcountry") // 出口目的国
 8     let destcountryName = commonUtil.getLookupName("new_destcountry") // 出口目的国
 9 
10     let returnAnalysisFetchXml = `<fetch mapping="logical" version="1.0">
11             <entity name="new_return_analysis_detail">
12                 <attribute name="new_name"/>
13                 <attribute name="new_product_fourthid"/>
14                 <link-entity name="new_product_fourth" from="new_product_fourthid" to="new_product_fourthid" alias="PF" link-type="inner">
15                     <attribute name="new_issale"/>
16                     <link-entity name="new_product_team" from="new_product_teamid" to="new_product_teamid" alias="PT" link-type="inner">
17                         <attribute name="new_restrictedsales"/>
18                     </link-entity>
19                 </link-entity>
20                 <filter type="and">
21                     <condition attribute="new_return_analysis" operator="eq" value="${returnAnalysisId}"/>
22                 </filter>
23             </entity>
24         </fetch>`;
25 
26     var url = `/new_return_analysis_details?fetchXml=${encodeURIComponent(returnAnalysisFetchXml.replace(/>\s+</g, '><'))}`;
27     commonUtil.queryWithUrl(url, (result) => {  
28         if (result.data.filter(r => r["PF.new_issale"] == false).length > 0) {
29             isSubmit = false;
30             Xrm.Utility.alertDialog(`提交失败 : 明细中有物料不可销售`);
31             return isSubmit;
32         }
33         let restrictedProdList = result.data.filter(r => r["PT.new_restrictedsales"] == 10).map(p => p._new_product_fourthid_value)
34         if (restrictedProdList.length == 0) {
35             return isSubmit;
36         }
37 
38         let productRegionRelationShipFetchXml = `<fetch mapping="logical" version="1.0">
39             <entity name="new_productregionrelationship">
40                 <attribute name="new_productcode" />
41                 <attribute name="new_country_region" />
42                 <filter type="and">
43                     <condition attribute="new_productcode" operator="in"><value>${restrictedProdList.join('</value><value>')}</value></condition >
44                     <condition attribute="new_country_region" operator="eq" value="${destcountryId}" />
45                 </filter>
46             </entity>
47         </fetch>`;
48 
49         var url = `/new_productregionrelationships?fetchXml=${encodeURIComponent(productRegionRelationShipFetchXml.replace(/>\s+</g, '><'))}`;
50         commonUtil.queryWithUrl(url, (results) => {  
51             // 限制区域的产品资源数量 != 查询到的【产品资源与销售区域的关系】数量
52             // 也就是存在当前出库目的国不可销售的产品资源
53             let canntSaleList = restrictedProdList.filter(p => !results.data.some(r =>r.new_productcode === p.id))
54             if (canntSaleList.length) {
55                 isSubmit = false;
56                 let prodNameList = result.data.filter(r => canntSaleList.indexOf(r._new_product_fourthid_value) >= 0).map(p => p.new_name)
57                 Xrm.Utility.alertDialog(`提交失败 : ${`收益分析明细[${prodNameList.join(",")}]在出口目的国[${destcountryName}]不可销售! `}`);
58             }
59         }, false);
60     }, false);
61     
62     return isSubmit;
63 }
简单的示例:
let productRegionRelationShipFetchXml = `<fetch mapping="logical" version="1.0">
            <entity name="new_productregionrelationship">
                <attribute name="new_productcode" />
                <attribute name="new_country_region" />
                <filter type="and">
                    <condition attribute="new_productcode" operator="in"><value>${restrictedProdList.join('</value><value>')}</value></condition >
                    <condition attribute="new_country_region" operator="eq" value="${destcountryId}" />
                </filter>
            </entity>
        </fetch>`;

        var url = `/new_productregionrelationships?fetchXml=${encodeURIComponent(productRegionRelationShipFetchXml.replace(/>\s+</g, '><'))}`;
主要注意的地方时在调用是fetchXML需要使用encodeURIComponent进行序列化