【问题标题】:How to query a keyword on different attributes based on entity in fetchXML?如何根据fetchXML中的实体查询不同属性的关键字?
【发布时间】:2026-01-20 12:05:02
【问题描述】:

我在数据库中有 2 个实体;约会和电子邮件。我想编写一个搜索函数来获取包含用户输入的字符串的所有约会和电子邮件。但是,我想根据它是哪个实体来搜索不同的属性。例如:我想获取所有属性 subject 包含字符串“meeting today”的约会,我还想获取所有属性 description 包含相同字符串的电子邮件。所以简单来说,只搜索约会的主题行,只搜索电子邮件的描述。

这是我的 fetchXml 到目前为止的样子:

<fetch count="10" distinct="true" mapping="logical" no-lock="true" output-format="xml-platform" page="1" returntotalrecordcount="false" version="1.0">
    <entity name="activitypointer">
        <attribute name="subject"/>
        <attribute name="description"/>
        <attribute name="activitytypecode"/>
        <filter type="or">
            <condition attribute="activitytypecode" operator="like">
                <value>Email</value>
            </condition>
            <filter type="and">
                <condition attribute="description" operator="like" value="%meeting today%"/>
            </filter>
        </filter>
        <filter type="or">
            <condition attribute="activitytypecode" operator="like">
                <value>Appointment</value>
            </condition>
            <filter type="and">
                <condition attribute="subject" operator="like" value="%meeting today%"/>
            </filter>
        </filter>
    </entity>
</fetch>

然而,这似乎并没有夺回任何记录。在查询单个实体类型时,我可以成功获取记录,但同时放置两个过滤器不会返回任何内容。我要求在 fetchXML 中做什么?还是我构建查询的方式有问题?

【问题讨论】:

    标签: dynamics-crm microsoft-dynamics dynamics-crm-2013 dynamics-365 fetchxml


    【解决方案1】:

    我刚刚在我的环境中做了一个快速测试,这是有效的。您可以在高级查找和下载 fetchxml 中构建此类查询,然后在 XrmToolBox FetchXml 构建器中进行编辑/测试。

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
      <entity name="activitypointer" >
        <attribute name="activitytypecode" />
        <attribute name="subject" />
        <filter type="and" >
          <filter type="or" >
            <filter type="and" >
              <condition attribute="activitytypecode" operator="eq" value="4202" />
              <condition attribute="subject" operator="like" value="%test%" />
            </filter>
            <filter type="and" >
              <condition attribute="activitytypecode" operator="eq" value="4201" />
              <condition attribute="description" operator="like" value="%test%" />
            </filter>
          </filter>
        </filter>
      </entity>
    </fetch>
    

    【讨论】: