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学习笔记 系列汇总

你可能感兴趣的:(dynamic)