在前面的章节中我们讨论了怎么通过JS来实现实体的CRUD操作,今天我们就来谈谈Read操作中的查询URL。Read操作是日常开发中最常用也是最重要的功能,Read的操作的核心内容就是查询URL的构造了。Dynamics CRM 2011因为是建立在WCF中的,而我们的Read操作就是通过JS来访问系统的数据服务。可以理解为:“查询URL”最终是用来告诉数据服务应该怎么去查找我们需要的数据,这和Sql语句中的Select语句类似。
我们来看几个小例子吧。首本章的编码会用到第8节《Dynamics CRM 2011 编程系列 (8):JS编程之实体CRUD辅助类》中的辅助类。
生成测试数据:
var path="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet"; var tmpAccount=new Object(); var helper for(var i=0;i<100;i++) { tmpAccount.Name="测试数据"+i.toString(); helper.Create(path,JSON.stringify(tmpAccount)); }
查询URL:
/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet
JS调用代码:
var query="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet"; var result=helper.Read(query); var displayMsg=""; //debugger; for(var index=0;index< result.results.length;index++) { displayMsg=displayMsg+String.format("[{0}]{1}",index,result.results[index].Name)+"\t"; if(index+1%2==0) { displayMsg=displayMsg+"\n"; } } displayMsg+=String.format("一共有{0}条数据",result.results.length); alert(displayMsg);
返回结果:
查询URL:/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?$top=5&$orderby=CreatedOn desc
JS调用代码:
var query="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?$top=5&$orderby=CreatedOn desc"; var result=helper.Read(query); var displayMsg=""; for(var index=0;index< result.results.length;index++) { displayMsg=displayMsg+String.format("[{0}]{1}---CreatedOn{2}",index,result.results[index].Name,new Date(parseInt(result.results[index].CreatedOn.replace (/\/Date\((\d+)\)\//,"$1") ) ) )+"\n"; } alert(displayMsg);
查询结果:
查询URL:
/gh2011/XRMServices/2011/OrganizationData.svc/AccountSet?$select=Name&$filter=startswith(Name,'%e6%9d%8e') eq true
JS调用代码:
var query="/gh2011/XRMServices/2011/OrganizationData.svc/AccountSet?$select=Name&$filter=startswith(Name,'%e6%9d%8e') eq true"; var result=helper.Read(query); var displayMsg=""; for(var tmp in result.results) { displayMsg+=String.format("客户:{0} \n",result.results[tmp].Name); } alert(displayMsg);
查询结果:
查询URL:
/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?select=Name&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B-000C292C54BE}')
JS调用代码:
var query="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?select=Name&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B- 000C292C54BE}')"; var result=helper.Read(query); var displayMsg=""; for(var index=0;index< result.results.length;index++) { displayMsg=displayMsg+String.format("[{0}]{1}",index,result.results[index].Name)+"\n"; } alert(displayMsg);
获取某记录ID的技巧:
步骤一
步骤二
查询结果:
查询URL:
/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?select=LastName&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B-000C292C54BE}')&$expand=account_primary_contact
JS调用代码:
var query="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet?select=LastName&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B- 000C292C54BE}')&$expand=account_primary_contact"; debugger; var result=helper.Read(query); var displayMsg=""; for(var index=0;index< result.results.length;index++) { displayMsg=displayMsg+String.format("{0}--电话:{1}" ,result.results[index].account_primary_contact.LastName+result.results [index].account_primary_contact.FirstName ,result.results[index].account_primary_contact.Telephone1 )+"\n"; } alert(displayMsg);
查询结果:
查询URL:
/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet
JS调用代码:
var query="/GH2011/XRMServices/2011/OrganizationData.svc/AccountSet"; var result=helper.Read(query); var displayMsg=""; var index=1; //debugger; while(true) { for(var tmp=0;tmp<result.results.length;tmp++) { displayMsg+=String.format("{0}{1}\t",index++,result.results[tmp].Name); if(index-1%2==0) { display+="\n"; } }//for if(result.__next!=undefined) { result=helper.Read(result.__next);//还有数据每有显示出来 } else { break; } }//while displayMsg+=String.format("一共{0}条数据",index-1); alert(displayMsg);
查询结果:
查询URL:
/gh2011/XRMServices/2011/OrganizationData.svc/AccountSet?
$select=Name,account_primary_contact/LastName,account_primary_contact/FirstName,account_primary_contact/Telephone1&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B-000C292C54BE}') and (startswith(Name,'%e5%8a%a8') eq true)&$expand=account_primary_contact
JS调用代码:
var query="/gh2011/XRMServices/2011/OrganizationData.svc/AccountSet? $select=Name,account_primary_contact/LastName,account_primary_contact/FirstName,account_primary_contact/Telephone1&$filter=PrimaryContactId/Id eq (guid'{8908336F-3F64-E111-844B-000C292C54BE}') and (startswith(Name,'%e5%8a%a8') eq true)&$expand=account_primary_contact"; var result=helper.Read(query); var displayMsg=""; for(var tmp in result.results) { displayMsg+=String.format("客户:{0} \t 联系人:{1} 联系人的商务电话:{2} \n",result.results[tmp].Name ,result.results[tmp].account_primary_contact.LastName+result.results [tmp].account_primary_contact.FirstName,result.results[tmp].account_primary_contact.Telephone1); } alert(displayMsg);
查询结果:
oData查询字符串的详细构造