一般情况下使用拓展工具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 shipmentId = commonUtil.delBrackets(Xrm.Page.data.entity.getId())

let fetchXml = `<fetch mapping="logical" version="1.0">

<entity name="new_box_single_detail">

<attribute name="new_customs_receipts"/>

<attribute name="new_is_erp"/>

<link-entity name="new_outbound_order_details" from="new_outbound_order_detailsid" to="new_out_detail" alias="OOD" link-type="inner"/>

<filter type="and">

<condition entityname="OOD" attribute="new_outbound_order" operator="eq" value="${shipmentId}"/>

</filter>

</entity>

</fetch>`

let boxDetailQuery = `/new_box_single_details?fetchXml=${encodeURIComponent(fetchXml.replace(/>\s+</g, '><'))}`

let showButton = false

commonUtil.queryWithUrl(boxDetailQuery, result => {

showButton = result.success && result.data.length

&& result.data.filter(d => !d.new_is_erp && d.new_customs_receipts).length > 0

}, false)

多条件主表与关联表都筛选的的fetchxml示例:
 1 let fetchXml = `<fetch mapping="logical" version="1.0">
2 <entity name="foton_kd_claimdetail">
3 <all-attributes />
4 <link-entity name="foton_kd_vehiclelib" from="foton_kd_vehiclelibid" to="foton_vehicle" alias="LIB" link-type="inner"/>
5 <filter type="and">
6 <condition entityname="LIB" attribute="foton_codefoton" operator="eq" value="${vehicleno}"/>
7 <condition attribute="foton_ftcontractno" operator="eq" value="${contractno}"/>
8 <filter type="or">
9 <filter type="and">
10 <condition attribute="foton_status" operator="eq" value="136020000"/>
11 <condition attribute="foton_addtype" operator="eq" value="136020001"/>
12 </filter>
13 <condition attribute="foton_status" operator="eq" value="136020005"/>
14 </filter>
15 </filter>
16 </entity>
17 </fetch>`
18 let queryStr = `/foton_kd_claimdetails?fetchXml=${encodeURIComponent(fetchXml.replace(/>\s+</g, '><'))}`
19 //queryStr += ` and ((foton_status eq 136020000 and foton_addtype eq 136020001) or foton_status eq 136020005)&$orderby=createdon desc,foton_nofoton asc`
20 commonUtil.queryWithUrl(queryStr, result => {
21 debugger
22 if (!result.success || !result.data || result.data.length < 1) {
23 Xrm.Utility.alertDialog("未能查询到索赔明细数据!")
24 return
25 }
26 }
主要注意的地方时在调用是fetchXML需要使用encodeURIComponent进行序列化

使用CRM REST Builder的Predefined Query在js结合FetchXML语句进行查询的更多相关文章

  1. Dynamics CRM REST Builder

    今天介绍个很棒的工具叫CRM REST Builder,不管是2016之前的odata查询或者现在的web api都不在话下,界面如下,选项非常丰富 这里以retrieve multiple举个例子, ...

  2. Elasticsearch Query DSL 整理总结(一)—— Query DSL 概要,MatchAllQuery,全文查询简述

    目录 引言 概要 Query and filter context Match All Query 全文查询 Full text queries 小结 参考文档 引言 虽然之前做过 elasticse ...

  3. crm使用FetchXml分组聚合查询

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月09号  */ namespace Net.CRM.FetchXml {     using System;     using Micr ...

  4. Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式

    Segregate operations that read data from operations that update data by using separate interfaces. T ...

  5. Dynamic CRM 2013学习笔记(十七)JS读写各种类型字段方法及技巧

    我们经常要对表单里各种类型的字段进行读取或赋值,下面列出各种类型的读写方法及注意事项: 1. lookup 类型 清空值 var state = Xrm.Page.getAttribute(" ...

  6. Flashback Query、Flashback Table(快速闪回查询、快速闪回表)

    Flashback Query闪回查询 flashback query是基于undo表空间的闪回,与之相关的参数如下: SQL> show parameter undo NAME         ...

  7. SQL Server捕获发生The query processor ran out of internal resources and could not produce a query plan...错误的SQL语句

    最近收到一SQL Server数据库服务器的告警邮件,告警内容具体如下所示: DATE/TIME: 10/23/2018 4:30:26 PM DESCRIPTION:  The query proc ...

  8. 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null

    报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...

  9. 通过redash query results 数据源实现跨数据库的查询

    redash 提供了一个简单的 query results 可以帮助我们进行跨数据源的查询处理 底层数据的存储是基于sqlite的,期望后期有调整(毕竟处理能力有限),同时 query results ...

  10. query.validate.js使用说明+中文API

    转自:http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html 看到一篇好的文章不容易,记录下来以防丢失! 官网地址:http:/ ...

随机推荐

  1. 栈与队列(c语言实现)

    文章目录 1.栈 1.1基于数组实现栈 1.1.1定义栈的结构体 1.1.2栈的初始化 1.1.3栈的释放 1.1.4元素入栈 1.1.5元素出栈 1.1.6访问栈顶元素 1.2基于链表实现栈 1.2 ...

  2. mysql弱密码爆破

    mySQL弱密码  靶场:/vulhub/mysql/CVE-2012-2122  启动: docker-compose up -d 扫描端口 nmap -Sv -Pn -T4 靶机ip  看到在33 ...

  3. Python311新特性-特化指令specializing adaptive interpreter-typing-asyncio

    Python3新特性 python3.11增加了许多特性,让python更快更加安全,本文从应用层面来讲一下python3.11的这些新特性 特化自适应解析器是什么,如何利用特化写出更高性能的代码 如 ...

  4. 制作一个ai丛雨(附Python代码)

    绫,再一次,再一次创造一个有你的世界 开一个随笔记录一下我的第一版ai老婆,目前只有普通对话和切换背景的功能(后面可能会加一个选人物功能) 先放一个效果图(看起来还行) 代码和注意事项都放在了下面,应 ...

  5. 移动端元素定位辅助神器-WEditor

    WEditor可以做什么? 编辑器能够提供辅助编写脚本,查看组件信息,调试代码等功能. 移动端除了用 Appium 来 做元素定位外,还可以通过 WEditor 来完成. 前置环境依赖按照 以 win ...

  6. 奥迪借助Karpenter为关键业务节省63%成本

    原文链接: https://aws.amazon.com/cn/solutions/case-studies/audi-efficient-compute-case-study/ 翻译:cloudpi ...

  7. markdown小小白常用语法

    第一次用vscode写笔记去同步Cnblog,不知道写啥就记点常用的md语法吧 1. 标题怎么写? 利用"#" + " " 即可实现第几节标题(其中'/',表转 ...

  8. 异步编程在ArkTS中具体怎么实现?

    大家好,我是 V 哥,很好奇,在ArkTS中实现异步编程是怎样的,今天的内容来聊聊这个问题,总结了一些学习笔记,分享给大家,在 ArkTS中实现异步编程主要可以通过以下几种方式: 1. 使用async ...

  9. 解读Graph+AI白皮书:LLM浪潮下,Graph尚有何为?

    历时半年,由蚂蚁集团和之江实验室牵头,联合北京邮电大学.浙江大学.西湖大学.东北大学.杭州悦数科技.浙江创邻科技.北京大学.北京交通大学.复旦大学.北京海致星图科技.腾讯.信雅达科技.北京枫清科技等单 ...

  10. 使用 LLVM 框架创建一个工作编译器,第 1 部分

    使用 LLVM 及其中间表示构建一个自定义编译器 LLVM 编译器基础架构提供了一种强大的方法来优化您使用任何编程语言编写的应用程序.了解本系列文章(由两部分组成)第一部分中有关 LLVM 的基础知识 ...