Share via


Outer join with Fetchxml in CRM 2013

Hello folks,

Here is the scenario:

I remember the days we used to write lengthy code to get all the entities that are not associated with another entity.

Did not understand?? Ok… let me give you an example: I want to get all the cases that are not having tasks associated with them. This is a complex way of getting until CRM 2011. Now things have changed so are we!! Excited?? So let us look into how we can achieve this requirement:

 

  1. First get the organization service, context of the record if you are using a plugin.

     

  2. In this example I want to get all the cases that are not having tasks associated with them. Here is the fetchxml code for achieving the same:

     

    string strFetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>

     

    <entity name='incident'>

     

    <attribute name='title' />

     

    <link-entity name='task' from='regardingobjectid' to='incidentid' alias='ab' link-type='outer'>

     

    <attribute name='regardingobjectid' />

     

    </link-entity>

     

    <filter type='and'>

     

    <condition entityname='ab' attribute='regardingobjectid' operator='null' />

     

    </filter>

     

    </entity>

     

    </fetch>";

     

In the above fetch xml you can see the link-type being shown as outer and we writing an alias for this entity. Eventually we are writing a condition mapping that alias name.

 

                    EntityCollection caseCollection = service.RetrieveMultiple(new
FetchExpression(strFetch));

foreach (Entity caseEntity in caseCollection.Entities)

 

{

 

// Create a task activity to follow up with the account customer in 7 days.

 

Entity followup = new
Entity("task");

 

 

 

 

followup["subject"] = "Send e-mail to the customer.";

 

followup["description"] =

 

"Follow up with the customer. Check if there are any new issues that need resolution.";

 

followup["scheduledstart"] = DateTime.Now;

 

followup["scheduledend"] = DateTime.Now.AddDays(7);

 

followup["category"] = context.PrimaryEntityName;

 

followup["regardingobjectid"] = new EntityReference("incident", caseEntity.Id);

 

 

service.Create(followup);

 

}

 

That is all!!You can relate this with several 1:n relationships that you have in CRM 2013.

Cheers!!

Vishnu

Comments

  • Anonymous
    December 22, 2013
    Pingback from Outer join with Fetchxml in CRM 2013 : Interesting Findings & Knowledge Sharing