Dynamic CRM 2013學習筆記(九)CrmFetchKit.js介紹:Fetchxml、多表聯合查詢, 批量更新


CrmFetchKit.js是一個跨瀏覽器的一個類庫,允許通過JavaScript來執行fetch xml的查詢,還可以實現批量更新,分頁查詢等。目前已支持Chrome 25, Firefox 19 和 IE9/10 .

它的最大優勢是可以通過fetchxml 來查詢,這樣我們就可以實現真正的多表聯合查詢,雖然可以用OData終結點的$expand來進行多表的聯合查詢,但這種方式沒辦法過濾多表的條件,它只能過濾主表的條件。

 

下面來看下簡單的多表查詢的例子:

1.首先定義一個fetchxml:

  1: var fetchxml = ['<fetch version="1.0" output-format="xml-platform" ',
  2:                     'mapping="logical" ',
  3:                     'returntotalrecordcount="true" >',
  4:                 '<entity name="new_floor_price">',
  5:                 '<attribute name="new_floor_priceid" />',
  6:                 '<order attribute="modifiedon" descending="true" />',
  7:                 '<filter type="and">',
  8:                     '<condition attribute="new_approval_status" operator="eq" value="3" />',
  9:                 '</filter>',
 10:                 '<link-entity name="new_fp_item" from="new_fp" to="new_floor_priceid" alias="ar">',
 11:                     '<attribute name="new_floor_price" />',
 12:                     '<filter type="and">',
 13:                     '<condition attribute="new_modelid" operator="eq" value="' + modelid + '" />',
 14:                     '</filter>',
 15:                 '</link-entity>',
 16:                 '</entity>',
 17:             '</fetch>'].join('');

我一般先寫好sql語句,然后在 http://sql2fetchxml.com/ 這里生成fetchxml 語句,再小改下參數就成了。

image

在上面的框里輸入SQL 語句,點擊 中間的Convert,就可以在下面的框里生成 Fetchxml的語句。

 

2. 調用很簡單:

  1: CrmFetchKit.Fetch(fetchxml).then(function (results) {
  2:         if(results!=null)
  3:             setAttributeValue("new_floor_price", Number(results[0].getValue("ar.new_floor_price")));
  4:     }, onFetchError);

 

3. 錯誤處理:

  1: function onFetchError(xhr, status, errorThrown) {
  2:     
  3:     var errormsg = $(xhr.responseXML).find('Message').text();
  4: 
  5:     alert('CrmFetchKit-Error occured: ' +  errormsg);
  6: }

 

4. 另一種寫法:

  1: // execute the query (async)
  2: CrmFetchKit.Fetch(fetchxml)
  3:   .fail(function(xhr, status, errorThrown){ 
  4: 
  5:       // get the error-message
  6:       var msg = $(xhr.responseXML).find('Message').text();
  7: 
  8:       alert('Error occured: ' + msg);
  9:   })
 10:   .done(function(results){
 11:     
 12:       var contactid = null;
 13: 
 14:       for( var i = 0, max = results.length; i < max; i++) {
 15: 
 16:     contactid = results[i].getValue('contactid');
 17: 
 18:     // Assign (async)
 19:     CrmFetchKit.Assign(contactid, 'contact', bId);
 20:       }
 21:   });

這個示例中,還介紹了CrmFetchKit.Assign的用法

CrmFetchKit.Assign(contactid, 'contact', bId);

這樣就可以實現批量更新了

 

5. FetchMore 用法

  1: var fetchxml = ['<fetch version="1.0" output-format="xml-platform" ',
  2:                         'mapping="logical" ',
  3:                         'returntotalrecordcount="true" ',
  4:                         'count="10">',
  5:                 ' <entity name="contact">',
  6:                 ' <attribute name="lastname" />',
  7:                 ' <attribute name="contactid" />',
  8:                 ' <filter type="and">',
  9:                 ' <condition attribute="lastname" operator="like" value="test%" />',
 10:                 ' </filter>',
 11:                 ' </entity>',
 12:                 '</fetch>'].join('');
 13: 
 14: CrmFetchKit.FetchMore(fetchxml).then(function (response) {
 15: 
 16:     // 獲取實體名
 17:     var entityname = response.entityName;
 18: 
 19:     // fetchxml里必須設置 'returntotalrecordcount="true" ' 
 20:     var total = response.moreRecords;
 21: 
 22:     // 取得實體的數組
 23:     var set = response.entities;
 24: 
 25:     // 分頁就需要這個 page-cookie
 26:     var pageCookie = response.pagingCookie;
 27:     
 28:     ...
 29: 
 30: }, onFetchError);

 

 

6. FetchAll用法:

  1: var fetchxml = ['<fetch version="1.0" output-format="xml-platform" ',
  2:                         'mapping="logical" ',
  3:                         'count="10">',
  4:                 ' <entity name="contact">',
  5:                 ' <attribute name="lastname" />',
  6:                 ' <attribute name="contactid" />',
  7:                 ' <filter type="and">',
  8:                 ' <condition attribute="lastname" operator="like" value="test%" />',
  9:                 ' </filter>',
 10:                 ' </entity>',
 11:                 '</fetch>'].join('');
 12: 
 13: CrmFetchKit.FetchAll(fetchxml).then(function (entities) {
 14: 
 15:     /* success-handler */
 16: 
 17: }, onFetchError);

 

7. 下載地址

Download  CRM Fetch Kit

 

注意事項:

1. 要注意全部是小寫

2. 給lookup賦值時,要注意lookup類型是正確的

 

Dynamic CRM 2013學習筆記 系列匯總


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM