Dynaimc CRM查找字段自定義過濾視圖:Xrm.Page.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)
實戰筆記
實作方式參考官方文檔提供的Xrm.Page.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)
參數的解釋:
viewId:具體的視圖id 可通過視圖編輯器窗體上方的網址中複制
entityName:實體名
viewDisplayName:視圖名稱,可自己編寫一個名稱
fetchXml:過濾查詢用的具體fetchXml語句,類似于編寫sql,可以通過線上sql轉fetchxml工具轉換( http://www.sql2fetchxml.com/)
layoutXml:定義視圖布局的 XML,格式比較固定,以一個比較簡單的為例:
layoutXml = '<grid name="resultset" object="10057" jump="foton_sequenceno" select="1" icon="1" preview="1"><row name="result" id="foton_receiptid"><cell name="foton_sequenceno" width="150" /><cell name="createdon" width="150" /></row></grid>';
注意:xml的連接配接符号如:'<''>'不要有空格
<grid>中name可固定為resultset,也可以定義成實體名
object為對應實體的ObjectTypeCode,資料庫查詢或者實體浏覽器中檢視
select用0或1都可以 0和1對應是否可以在視圖中進行查詢
icon和preview固定1
<row>中的name固定result即可,id為對應的實體主鍵
<cell>為具體要顯示的列配置,name為字段名,width為顯示的寬度
isDefault:視圖是否應該是預設視圖 預設設定為true即可
項目中的使用示例:
1.給字段添加onchange事件
1 /**
2 * 選擇産品代碼後,根據産品資源與包裝方式的關系過濾包裝方式可選擇的資料,添加自定義過濾視圖
3 * @param {any} productFourthId 産品代碼資料id
4 */
5 function addPostingLookupFilterCustomView(productFourthId) {
6 if (productFourthId == null) {
7 return
8 }
9 let fetchXml = '<fetch version="1.0" mapping="logical"><entity name="new_way_of_packaging"><attribute name="new_way_of_packagingid" /><attribute name="new_name" /><attribute name="new_cost" /><attribute name="createdon" /><link-entity name="new_product_packaging_relationship" from="new_way_of_packagingid" to="new_way_of_packagingid" alias="ship" link-type="inner"><filter type="and"><condition attribute="new_product_fourthid" operator="eq" value="' + commonUtil.delBrackets(productFourthId) + '" /></filter></link-entity></entity></fetch>';
10 let layoutXml = '<grid name="" object="10047" jump="new_name" select="1" icon="1" preview="0"><row name="new_way_of_packaging" id="new_way_of_packagingid"><cell name="new_name" width="300" /><cell name="new_cost" width="200" /><cell name="createdon" width="150" /></row></grid>';
11 Xrm.Page.getControl("new_way_of_packaging").addCustomView("{DB40ABE7-FB8D-4E41-ACF8-7569ECEAB149}", "new_way_of_packaging", "包裝方式過濾産品資源查詢", fetchXml, layoutXml, true);
12 }
2.在頁面加載時做判斷顯示視圖 結合查找字段addPreSearch的方法
1 //窗體onload函數彙總調用方法
2
3 /**
4 *過濾源、目标經銷商來款可選視圖
5 * @param {any} type 1轉出賬戶 2轉入賬戶
6 */
7 function preAccountToParagraphFilterLookup(type) {
8 if (!type) {
9 return
10 }
11 if (type == 1) {
12 Xrm.Page.getControl("new_lkaccount").addPreSearch(function () {
13 addPostingLookupFilterCustomView(type)
14 })
15 } else {
16 Xrm.Page.getControl("new_targetlkaccount").addPreSearch(function () {
17 addPostingLookupFilterCustomView(type)
18 })
19 }
20 }
21 /**
22 * 添加過濾源、目标經銷商來款可選擇的自定義視圖
23 * @param {any} type 1轉出賬戶 2轉入賬戶
24 */
25 function addPostingLookupFilterCustomView(type) {
27 var fetchXml = '';
28 let amountId = null
29 if (type == 1) {
30 amountId = Xrm.Page.getAttribute("new_amountid_from").getValue()
31 amountId = amountId ? amountId[0].id : null
32 if (amountId == null) {
33 return
34 }
35 } else {
36 amountId = Xrm.Page.getAttribute("new_amountid_to").getValue()
37 amountId = amountId ? amountId[0].id : null
38 if (amountId == null) {
39 return
40 }
41 }
42 fetchXml = '<fetch version="1.0" mapping="logical"><entity name="new_lkaccount"><attribute name="new_lkaccountid" /><attribute name="new_name" /><attribute name="createdon" /><filter type="and"><condition attribute="new_approvalstatus" operator="eq" value="40" /><filter type="or"><condition attribute="new_amountid_capital" operator="eq" value="' + commonUtil.delBrackets(amountId) + '" /><condition attribute="new_amountid_parts" operator="eq" value="' + commonUtil.delBrackets(amountId) + '" /></filter></filter></entity></fetch>';
43 var layoutXml = '<grid name="" object="10174" jump="new_name" select="1" icon="1" preview="0"><row name="new_lkaccount" id="new_lkaccountid"><cell name="new_name" width="300" /><cell name="createdon" width="150" /></row></grid>';
44
45 if (type == 1) {
46 Xrm.Page.getControl("new_lkaccount").addCustomView("{4F08B504-491C-473F-B215-FF99894870D9}", "new_lkaccount", "經銷商來款過濾資金賬戶查詢", fetchXml, layoutXml, true);
47 } else {
48 Xrm.Page.getControl("new_targetlkaccount").addCustomView("{4F08B504-491C-473F-B215-FF99894870D9}", "new_lkaccount", "經銷商來款過濾資金賬戶查詢", fetchXml, layoutXml, true);
49 }
50 }