web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to use filter on link entity

(0) ShareShare
ReportReport
Posted on by 645

Hey,
I'm trying to get a product records according to linkentity filtering, I need to get all products that has Active productstatus and the linked phonecall category is fun and not open:

AKA : PRODUCT - (productstatus = Active)& (linkedphonecallcategory = fun && linkedphonecallstatus != open)

when I run my current query, the results I get are without the liked entity filter... I don't understand why.
That's my code:

QueryExpression query = new QueryExpression("product");
query.Criteria.AddCondition(new ConditionExpression("productstatus", ConditionOperator.Equal, 0));
query.ColumnSet = new ColumnSet("product");
LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter);
FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
filter1.Conditions.Add(new ConditionExpression("phonecallcategory", ConditionOperator.Equal, GetConfigByKey("fun")));
filter1.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));

phoneCallLink.LinkCriteria = filter1;

    query.LinkEntities.Add(phoneCallLink);
phoneCallLink.EntityAlias = "products";
EntityCollection AllProductsWithSpecificCallsNotOpen = new EntityCollection();

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Friyank Profile Picture
    944 on at

    Are you using System Entity or custom Entity of product

    I Triggered Same Query

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

               #region Connect To CRM in just one lines

               CrmServiceClient Crmconn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineUrl"].ConnectionString);

               #endregion

               if (!Crmconn.IsReady)

               {

                   Console.WriteLine("No Connection was Made.");

                   return;

               }

               QueryExpression query = new QueryExpression("product");

               query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));

               query.ColumnSet = new ColumnSet("productid");

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

               FilterExpression filter1 = new FilterExpression(LogicalOperator.And);

               filter1.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));

               phoneCallLink.LinkCriteria = filter1;

               phoneCallLink.EntityAlias = "products";

               EntityCollection AllProductsWithSpecificCallsNotOpen = new EntityCollection();

               var data = Crmconn.RetrieveMultiple(query);

    It worked and returned Records as well

    I made two changes in query

    Changed

    query.Criteria.AddCondition(new ConditionExpression("productstatus", ConditionOperator.Equal, 0));

    To

     query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));

    and

    Changed

    query.ColumnSet = new ColumnSet("product");

    To

    query.ColumnSet = new ColumnSet("productid");

  • Suggested answer
    Shidin Haridas Profile Picture
    3,497 on at

    Your code seems fine and should be working as expected, except for this line of code:

    8877.Capture.PNG

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Below is an example. Just modify it to work for your entities:

    private EntityCollection RetrieveListMembers(Guid listid)
    {
       QueryExpression query = new QueryExpression()
       {
          EntityName = "contact",
          ColumnSet = new ColumnSet(new string[] { "contactid", "fullname", "firstname", "lastname", "emailaddress1", "donotemail", "statecode" }),
          LinkEntities =
          {
             new LinkEntity()
             {
                LinkToEntityName = "listmember",
                LinkToAttributeName = "entityid",
                LinkFromEntityName = "contact",
                LinkFromAttributeName = "contactid",
                LinkCriteria =
                {
                   Conditions =
                   {
                      new ConditionExpression("listid", ConditionOperator.Equal, listid)
                   }
                }
             }
          }
       };

       try
       {
          EntityCollection rc = service.RetrieveMultiple(query);
          return rc;
       }
       catch (FaultException<OrganizationServiceFault> ex)
       {
          throw new InvalidPluginExecutionException("An error occurred in the plug-in. ", ex);
       }
    }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans