微软动态CRM专家罗勇 ,回复333或者20190508可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
先举个N:N关系的例子。这里以根据团队的名称查找其所有团队成员的主要邮箱为例。用Web API ODATA语法执行fetchxml比较容易想到,如下:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/systemusers?fetchXml=<fetch mapping='logical' no-lock='true'><entity name='systemuser'><attribute name='internalemailaddress' /><link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'><link-entity name='team' from='teamid' to='teamid' alias='ab'><filter type='and'><condition attribute='name' operator='eq' value='B2C Supervisor Team' /></filter></link-entity></link-entity></entity></fetch>
也可以使用如下的查询来解决,你可能会问下面查询语法中的 teammembership_association 从哪儿来,就是该实体的N:N关系的 Schema Name。
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams?$select=name&$expand=teammembership_association($select=internalemailaddress)&$filter=name eq 'B2C Supervisor Team'
还可以使用两次查询来解决,如下,当然这个方法比较笨。
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams?$filter=name eq 'B2C Supervisor Team'
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams(
e4336d83-f45f-e911-a830-000d3a375590)/teammembership_association?$select=internalemailaddress
如果换成根据队列(Queue)的名称来查看队列成员呢,分别如下:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/systemusers?fetchXml=<fetch mapping='logical' nolock='true'><entity name='systemuser'><attribute name='internalemailaddress' /><link-entity name='queuemembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'><link-entity name='queue' from='queueid' to='queueid' alias='ac'><filter type='and'><condition attribute='name' operator='eq' value='DS Approver IDG' /></filter></link-entity></link-entity></entity></fetch>
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/queues?$select=name&$expand=queuemembership_association($select=internalemailaddress)&$filter=name eq 'DS Approver IDG'
如果是1:N关系,可以参考如下语法,注意父实体一定要指定至少一个列,否则会把父实体的所有字段查出来,很浪费性能。$expand后面的名称也是1:N关系的架构名称。
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/new_returnreqs?$select=new_name&$expand=new_new_returnreq_new_returnreqapprovalhist_ReturnRequest($select=new_name)&$filter=contains(new_name,'RETURN-2019')
如果已知父记录的ID,则还有如下语法可以使用:
当仅仅需要返回子记录的ID的话,有简单语法如下:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/new_returnreqs(709c2ad3-7d71-e911-a82b-000d3a365662)/new_new_returnreq_new_returnreqapprovalhist_ReturnRequest/$ref
当仅仅需要返回子记录数量的话,有如下简单语法:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/new_returnreqs(709c2ad3-7d71-e911-a82b-000d3a365662)/new_new_returnreq_new_returnreqapprovalhist_ReturnRequest/$count
在查询记录的时候可以顺路查出其某个查找字段对应查找记录的字段信息吗?答案是可以。如下实例,这里使用查找字段的架构名称:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/new_returnreqapprovalhists?$select=new_name&$expand=new_ReturnRequest($select=new_name,new_ordernumber)&$filter=contains(new_name,'2019')
但是对于标准的CreatedBy字段却要使用逻辑名称,奇怪:
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/new_returnreqapprovalhists?$select=new_name&$expand=createdby($select=fullname)&$filter=contains(new_name,'2019')
查询某个是否在某个团队中可以用下来的语法,看返回数据中 teammembership_association 元素的长度就知道,等于0就是不在这个团队中,否则就是在。
https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams( b48fc02d-3431-ea11-a810-000d3a378457)?$select=teamid&$expand=teammembership_association($filter=systemuserid eq '6df07104-1ad3-e911-a9a2-000d3a324a3e') https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams?$select=name&$filter=name eq 'myteam'&$expand=teammembership_association($filter=systemuserid eq '6df07104-1ad3-e911-a9a2-000d3a324a3e') https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams?$select=name&$filter=name eq 'RM ISSR JP Team'&$expand=teammembership_association($filter=domainname eq 'luoyong@crm.onmicrosoft.com') https://luoyongdemo.api.crm.dynamics.com/api/data/v9.1/teams?$select=name&$filter= teamid eq '6f023228-b58e-ea11-a811-000d3a378a3a' or teamid eq 'b48fc02d-3431-ea11-a810-000d3a378457' or teamid eq '983e0d75-fd83-ea11-a812-000d3a33be69' or teamid eq '2ea3f893-fd83-ea11-a812-000d3a33be69'&$expand=teammembership_association($filter=systemuserid eq '6df07104-1ad3-e911-a9a2-000d3a324a3e')