天天看點

Dynamic CRM使用FetchXML在js中查詢與調用傳遞編碼問題

Dynamic CRM使用FetchXML在js中查詢與調用傳遞編碼問題

在頁面互動腳本js中實作窗體互動邏輯是很常見的crm場景,一般情況下使用拓展工具RESTBuilder編輯器,可以很友善的進行操作,增删改查均能實作,但在某些較為特殊的場景下,需要根據條件去拼接查詢過濾條件的,使用編輯器生成的代碼無法實作,需要結合使用fetchXML,當然這個編輯器中也拓展了結合fetchxml的方式,跟webapi調用一樣隻不過需要傳遞fetchxml,項目中使用了一個封裝好的JS工具庫XrmServiceToolkit.js,使用起來更友善,直接調用方法和傳遞fetchxml即可。

方法如下:

1.解決方案中添加XrmServiceToolkit.js Web資源 

2.業務實體的窗體資源中添加XrmServiceToolkit.js

3.業務實體窗體單獨的互動js腳本中 fetchXML方式調用查詢

1 var moduleid = Xrm.Page.data.entity.getId();
 2     var fetchXML = '<fetch mapping="logical" version="1.0">'
 3         + '<entity name="crm_ordermanage">'
 4         + '<attribute name="crm_name" />'
 5         + '<filter>'
 6         + '<condition attribute="crm_ordermanageid" operator="eq" value="' + moduleid + '" />'
 7         + '</filter>'
 8         + '<link-entity name="opportunity" from="opportunityid" to="crm_opportunity" alias="b" link-type="inner">'
 9         + '<attribute name="crm_oldproject" />'
10         + '<attribute name="crm_approvalstatus" />'
11         + '<filter>'
12         + '<condition attribute="crm_oldproject" operator="not-null" />'
13         + '<condition attribute="crm_approvalstatus" operator="eq" value="171060002" />'
14         + '</filter>'
15         + '</link-entity>'
16         + '</entity>'
17         + '</fetch>'
18     var query = XrmServiceToolkit.Soap.Fetch(fetchXML);
19     if (query.length <= 0) {
20         Xrm.Utility.alertDialog("查詢失敗!");
21         return;
22     }      

可以把查詢的FetchXML單獨弄一個方法,并且查詢的過濾條件可以都放到最下面,也可以在單獨link的實體裡單獨過濾,放到最下方過濾的方式如下:

1 function getOrderManagerFetchXML(orderdetailid) {
 2     var fetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'
 3         + '<entity name = "crm_ordermanage" >'
 4         + '<attribute name="crm_orderclassification" />'
 5         + '<link-entity name="crm_ordermanagedetails" from="crm_ordermanage" to="crm_ordermanageid" alias="ab" link-type="outer">'
 6         + '<attribute name="crm_ordermanagedetailsid" />'
 7         + '</link-entity>'
 8         + '<filter>'
 9         + '<condition entityname="ab" attribute="crm_ordermanagedetailsid"  value="' + orderdetailid + '" operator="eq" />'
10         + '</filter>'
11         + '</entity >'
12         + '</fetch >';
13     return fetchXML;
14 }      

調用傳值編碼問題:

在js中寫的fetchXML語句,在發起調用接口前,都需要先用encodeURIComponent轉義一下

但在轉義後,原先進行模糊查詢的過濾條件就偶爾調用不成功,後來發現是0開頭或者9結尾的都不能查詢 查詢後發現%連接配接0或9%都在轉義後有特殊意義 是以才會報錯無效的xml格式 最後找到解決辦法

%的後面加上25 即将原來的%替換成%25 問題就解決了

Dynamic CRM使用FetchXML在js中查詢與調用傳遞編碼問題