MS CRM 2011获取XML查询

时间:2013-02-26 23:37:44

标签: dynamics-crm-2011 fetchxml

我正在尝试为CRM 2011创建一些fetch xml或查询表达式。

我想OR下面的两个链接实体节点。这是否可行,我需要在一个请求中完成。

如果我可以执行此查询,我打算通过注入额外的标准来修改Activity History RetrieveMultiple视图事件,类似于下面的内容。

<fetch  mapping='logical' distinct='true'>
  <entity name='activitypointer'>
    <attribute name='activitytypecode' />
    <attribute name='subject' />
    <attribute name='statecode' />
    <attribute name='prioritycode' />
    <attribute name='modifiedon' />
    <attribute name='activityid' />
    <attribute name='instancetypecode' />
    <order attribute='modifiedon' descending='false' />
    <filter type='and'>
      <condition attribute='statecode' operator='eq' value='1' />
    </filter>
    <link-entity name='activityparty' from='activityid' to='activityid' alias='aa'>
      <link-entity name='account' from='accountid' to='partyid' alias='ab'>
        <filter type='or'>
          <condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
        </filter>
      </link-entity>
    </link-entity>
    <link-entity name='connection' from='record2id' to='activityid' alias='ad'>
      <link-entity name='account' from='accountid' to='record1id' alias='ae'>
        <filter type='or'>
          <condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
        </filter>
      </link-entity>
    </link-entity>
   </filter>
  </entity>
</fetch>

这是带回我正在寻找的结果的SQL,请注意问题是重新创建where子句的fetchXml,特别是检查其中一个连接是否存在。

SELECT DISTINCT
    ap.activitytypecode,
    ap.[subject],
    ap.statecode,
    ap.prioritycode,
    ap.modifiedon,
    ap.activityid,
    ap.instancetypecode

FROM 

dbo.FilteredActivityPointer ap

--First Link
LEFT OUTER JOIN dbo.FilteredActivityParty party
ON ap.activityid = party.activityid
AND party.partyid = '756CE4E9-F6F0-E111-8948-000C297B9BDA'

--Second Link
LEFT OUTER JOIN dbo.FilteredConnection connection
ON ap.activityid = connection.record2id
AND connection.record1id = '756CE4E9-F6F0-E111-8948-000C297B9BDA'

WHERE
    ap.statecode =1

    AND (
        NOT party.partyid IS NULL
     OR NOT connection.record1id IS NULL
    )
ORDER BY 
    ap.modifiedon

请帮忙。

1 个答案:

答案 0 :(得分:1)

如果我离开了标记,也许你可以编写你想要实现的基本SQL语句。但我认为您想要返回特定帐户是连接或活动方的所有活动指针?如果是这样,那么您需要将链接更改为外部链接。

尝试将link-type ='outer'属性和值添加到链接实体中,如下所示:

<fetch  mapping='logical' distinct='true'>
  <entity name='activitypointer'>
    <attribute name='activitytypecode' />
    <attribute name='subject' />
    <attribute name='statecode' />
    <attribute name='prioritycode' />
    <attribute name='modifiedon' />
    <attribute name='activityid' />
    <attribute name='instancetypecode' />
    <order attribute='modifiedon' descending='false' />
    <filter type='and'>
      <condition attribute='statecode' operator='eq' value='1' />
    </filter>
    <link-entity name='activityparty' from='activityid' to='activityid' alias='aa' link-type='outer'>
      <link-entity name='account' from='accountid' to='partyid' alias='ab' link-type='outer'>
        <filter type='or'>
          <condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
        </filter>
      </link-entity>
    </link-entity>
    <link-entity name='connection' from='record2id' to='activityid' alias='ad' link-type='outer'>
      <link-entity name='account' from='accountid' to='record2id' alias='ae' link-type='outer'>
        <filter type='or'>
          <condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
        </filter>
      </link-entity>
    </link-entity>
   </filter>
  </entity>
</fetch>

编辑1

在查看了您的SQL查询之后,我相信在单个查询中CRM不支持您尝试做的事情,虽然现在我找不到任何文档来支持我的预感......我不知道t认为你可以对外连接记录进行计数,然后说,只返回第一个链接实体的计数+第二个链接实体的计数&gt; 0,但我可能会弄错。