Dynamics CRM - 在 JS 中使用 QueryExpression
在上一篇博客中,介紹了 QueryExpression 在 C# plugin 中的使用,本篇主要介紹一下 QueryExpression 在 JS 中的使用,代碼示例如下:
1 //注意:取guid時要把花括號去掉 2 var currentId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", ""); 3 var pledgeId = Xrm.Page.getAttribute("cus_pledge_no").getValue()[0].id.replace("{", "").replace("}", ""); 4 5 var q = new Sdk.Query.QueryExpression("account"); 6 q.setDistinct(true); 7 q.setColumnSet("cus_donor_no"); 8 9 //添加主表條件 10 var queryFilter = new Sdk.Query.FilterExpression(Sdk.Query.LogicalOperator.And); 11 queryFilter.addCondition(new Sdk.Query.ConditionExpression("account", "cus_status", Sdk.Query.ConditionOperator.Equal, Sdk.Query.Ints([972050001]))); 12 queryFilter.addCondition(new Sdk.Query.ConditionExpression("account", "accountid", Sdk.Query.ConditionOperator.NotEqual, Sdk.Query.Guids([currentId]))); 13 q.setCriteria(queryFilter); 14 15 //綁定子表查詢 16 //note: this.LinkEntity = function (linkFromEntityName, linkToEntityName, linkFromAttributeName, linkToAttributeName, joinOperator, entityAlias) 17 var le = new Sdk.Query.LinkEntity("account", "contact", "accountid", "cus_account_no", Sdk.Query.JoinOperator.Inner, "temp"); 18 19 //添加子表條件 20 var linkFilter = new Sdk.Query.FilterExpression(Sdk.Query.LogicalOperator.And); 21 linkFilter.addCondition(new Sdk.Query.ConditionExpression("contact", "cus_status", Sdk.Query.ConditionOperator.Equal, Sdk.Query.Ints([972050000]))); 22 linkFilter.addCondition(new Sdk.Query.ConditionExpression("cus_pledgetobeterminated", "cus_pledge_no", Sdk.Query.ConditionOperator.Equal, Sdk.Query.Guids([pledgeId]))); 23 le.setLinkCriteria(linkFilter); 24 //設置要查詢的子表字段 25 le.setColumns("cus_remark"); 26 q.addLink(le); 27 28 //執行查詢 29 Sdk.Async.retrieveMultiple(q, function (cResult) 30 { 31 var aResult = cResult.getEntities().toArray(); 32 for (var _i = 0; _i < aResult.length; _i++) { 33 try { 34 var _e = aResult[_i]; 35 var oView = _e.view(); 36 //console.log(_e.getId()); 37 38 // Donor No. 39 var sDonorNo = ""; 40 if (oView.attributes.hasOwnProperty('cus_donor_no')) 41 sDonorNo = _e.getValue("cus_donor_no"); 42 } catch (err) { 43 console.error(err); 44 }
45 } 46 }