首页 > 解决方案 > 如何在链接实体上使用过滤器?

问题描述

我正在使用 crm 2016 并尝试根据链接性过滤获取产品(自定义实体)记录,我需要获取所有具有 Active productstatus 的产品并且链接电话类别为:funnot open

产品 - (productstatus = Active) & (linkedphonecallcategory = fun && linkedphonecallstatus != open)

当我运行当前查询时,我得到的结果没有链接实体过滤器。我不明白为什么。

这是我的代码:

FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
filter1.Conditions.Add(new ConditionExpression("phonecallcategory", ConditionOperator.Equal, "fun"));
filter1.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));

LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter);
phoneCallLink.LinkCriteria = filter1;
phoneCallLink.EntityAlias = "products";

QueryExpression query = new QueryExpression("product");
query.ColumnSet = new ColumnSet("productname");
query.LinkEntities.Add(phoneCallLink);
query.Criteria.AddCondition(new ConditionExpression("productstatus", ConditionOperator.Equal, 0)); 

EntityCollection AllProductsWithSpecificCallsNotOpen = new EntityCollection();

标签: c#filterdynamics-crmdynamics-crm-2016query-expressions

解决方案


你可以看看这里,我已经重组了你的查询,使其易于理解

示例:带有 LinkEntity 的 QueryExpression

//Link
LinkEntity phoneCallLink = new LinkEntity();
phoneCallLink.LinkFromEntityName  = "new_product";
phoneCallLink.LinkToEntityName = "phonecall";
phoneCallLink.LinkFromAttributeName = "new_productid";
phoneCallLink.LinkToAttributeName = "regardingobjectid";
phoneCallLink.JoinOperator = JoinOperator.LeftOuter;
phoneCallLink.EntityAlias = "phone";
phoneCallLink.LinkCriteria.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));
phoneCallLink.LinkCriteria.Conditions.Add(new ConditionExpression("phonecallcategory", ConditionOperator.Equal, "fun"));

//Query
QueryExpression query = new QueryExpression("new_product");
query.ColumnSet = new ColumnSet("new_name");
query.LinkEntities.Add(phoneCallLink);
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0)); 

//Execute
var AllProductsWithSpecificCallsNotOpen = service.RetrieveMultiple(query);

如果这仍然不起作用,您可以构建和导出fetchXml查询并将其转换为查询表达式:

示例:将 FetchXml 转换为 QueryExprssion

    string fetchXml =
    @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='account'>
    <attribute name='name' />
    <attribute name='accountid' />
    <order attribute='createdon' descending='true' />
    <order attribute='modifiedon' descending='true' />
    <filter type='and'>
      <condition attribute='createdon' operator='this-month' />
    </filter>
  </entity>
</fetch>";
    
    // Convert the FetchXML into a query expression.
    var conversionRequest = new FetchXmlToQueryExpressionRequest();
    conversionRequest.FetchXml = fetchXml; //fetchXml string

    var conversionResponse =(FetchXmlToQueryExpressionResponse)service.Execute(conversionRequest);
    var result = conversionResponse.Results.FirstOrDefault();

推荐阅读