crm 4.0 fetchxml example with Filtered View

Posted on Posted in MS Dynamics, MS Dynamics CRM

crm 4.0 fetchxml example with Filtered View

Let’s say we need to display some data which is from three different entities.

Our main entity is Campaign Response and other too entities are Campaign and Customer.

First line we need start with is below..

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

</fetch>

In above code we starting and closing fetch query. Next thing we need to do is include our main entity which is Campaign Response so above code becomes….

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”campaignresponse”>

</entity>

</fetch>

We haven’t done much above, all we did is added our main entity.

Next we need to include attributes we want to bring back. To keep it simple we will only fetch

  • Actual Start date
  • Regardingobjectid (an attribute of lookup type which relates to a different entity)

 

So by adding above two code will look like

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”campaignresponse”>

<attribute name=”regardingobjectid”/>

<attribute name=”actualstart”/> 

</entity>

</fetch>

Current code will bring back above two attributes for all records from campaign response entity. This is no good we want to filter on the basis of customer id (related to customer entity – you can use any entity this is just an example)

To join an entity we start with <link-entity>

<link-entity name=”customer” from=”customerid” to=” campaignresponse_customerid” visible=”false” link-type=”inner” alias=”FC”>

<attribute name=”customerid “/>                          

<filter type=”and”>

<condition attribute=”statecode” operator=”eq” value=”0″/>

<condition attribute=”customerid ” operator=”eq” param=”id” />

</filter>

</link-entity>

In above code ‘from’ is the primary key of our linked entity i.e. customer, ‘to’ is the attribute of our campaign response entity

At this stage we can search of campaigns related to a customer by passing a customer id.

Now move to second relationship we have with campaign entity

<link-entity name=”campaign” from=”campaignid” to=”regardingobjectid” visible=”false” link-type=”inner” alias=”CRC”>

<attribute name=”statuscode” />

<attribute name=”msa_eventtype” />

</link-entity>

Again this is another relationship we can add.

Below is the complete code how it looks like

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”campaignresponse”>

<attribute name=”regardingobjectid”/>

<attribute name=”actualstart”/> 

<link-entity name=”customer” from=”customerid” to=” campaignresponse_customerid” visible=”false” link-type=”inner” alias=”FC”>

<attribute name=”customerid “/>                          

<filter type=”and”>

<condition attribute=”statecode” operator=”eq” value=”0″/>

<condition attribute=”customerid ” operator=”eq” param=”id” />

</filter>

</link-entity>

<link-entity name=”campaign” from=”campaignid” to=”regardingobjectid” visible=”false” link-type=”inner” alias=”CRC”>

<attribute name=”statuscode” />

<attribute name=”msa_eventtype” />

</link-entity>

</entity>

</fetch>

 

Once we run above we should get all the attributes included in query.