Generate Fetchxml Condition in CRM Advanced Find using Fetchxml Query

Hello Everyone, those who are working in Dynamics CRM you will be aware that using advanced find you can able to ‘Download Fetch XML’ but how about uploading fetchxml query and generating condition is it possible ? 

Yes, it is possible but it will work only with Internet Explorer. Also need to do little bit hacking in debugger to acheive this.Please follow the below steps below to achieve this.

1.     Let’s take the below FetchXML as a reference which is already downloaded from CRM Advanced find for Opportunity Entity:

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

 <entity name="opportunity">

   <attribute name="statecode" />

   <attribute name="estimatedvalue" />

   <attribute name="estimatedclosedate" />

   <attribute name="actualclosedate" />

   <attribute name="name" />

   <attribute name="opportunityid" />

   <attribute name="modifiedon" />

   <order attribute="statecode" descending="false" />

   <order attribute="estimatedclosedate" descending="true" />

   <order attribute="modifiedon" descending="true" />

   <filter type="and">

     <filter type="or">

       <condition attribute="statecode" operator="eq" value="0" />

       <filter type="and">

        <condition attribute="actualclosedate" operator="last-x-months" value="12" />

         <condition attribute="statecode" operator="eq" value="1" />

       </filter>

     </filter>

   </filter>

 </entity>

</fetch>

2.   Open FetchXML query with Notepad++. If you don’t have Notepad++ you can download from their website : https://notepad-plus-plus.org/

3.     Once after Opening with Notepad++, now we need to convert the FetchXML into Single Line. In XML file, Press CTRL +A and then CTRL + J  

4.     It will be looking like this, after converting into single line:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  <entity name="opportunity">    <attribute name="statecode" />    <attribute name="estimatedvalue" />    <attribute name="estimatedclosedate" />    <attribute name="actualclosedate" />    <attribute name="name" />    <attribute name="opportunityid" />    <attribute name="modifiedon" />    <order attribute="statecode" descending="false" />    <order attribute="estimatedclosedate" descending="true" />    <order attribute="modifiedon" descending="true" />    <filter type="and">      <filter type="or">        <condition attribute="statecode" operator="eq" value="0" />        <filter type="and">          <condition attribute="actualclosedate" operator="last-x-months" value="12" />          <condition attribute="statecode" operator="eq" value="1" />        </filter>      </filter>    </filter>  </entity> </fetch>

5.     After Converting into Single Line, we need additional line to convert it as Javascript string variable. To do this, declare name as fetchxmlquery=' in first line and in the lastline add following '; (Single colon and Semicolon)

6.     It will be looking like this, after adding the javascript variable

fetchxmlquery='<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  <entity name="opportunity">    <attribute name="statecode" />    <attribute name="estimatedvalue" />    <attribute name="estimatedclosedate" />    <attribute name="actualclosedate" />    <attribute name="name" />    <attribute name="opportunityid" />     <attribute name="modifiedon" />    <order attribute="statecode" descending="false" />    <order attribute="estimatedclosedate" descending="true" />    <order attribute="modifiedon" descending="true" />    <filter type="and">      <filter type="or">        <condition attribute="statecode" operator="eq" value="0" />        <filter type="and">          <condition attribute="actualclosedate" operator="last-x-months" value="12" />          <condition attribute="statecode" operator="eq" value="1" />         </filter>      </filter>    </filter>  </entity> </fetch>';

7. Open Advanced Find in CRM, select the opportunity entity and also make sure it should be blank condition. Please check the below screenshot below:

8.So already you have open the Internet Explorer, press F12 Key and switch to Console Tab:

9. Now copy and paste the JavaScript variable fetchxmlquery (Refer Point 6). It should look something like this:

10.     Press the Execute Button, refer the screenshot below: 

11.It should Look like this, after executing:

12.     Clear the value in Console, it should look this:

13.Here comes the Magical Javascript, which will generate the condition in Advanced find. Type the Following, in Debugger 

contentIFrame0.$find('advFind').set_fetchXml(fetchxmlquery);

14.     It should like this, refer the screenshot below and press the execute button:

15.     Boom, you can see the advanced find filled with the condition:




I don't see the "execute" button...

Like
Reply

This is great! Is there anyway to get the fields added in 'edit columns' to also copy using this method? so far in my testing, the query is coming through but any adjustments made to the columns are not. thanks!

Like
Reply

Fantastic magic! Life savior! Many thanks. Is it possible to do it in Chrome?

Like
Reply

To view or add a comment, sign in

More articles by Sathis Kumar

  • HTML5 Signature Pad in Dynamics CRM/365 :

    This blog demonstrates how to display the Signature Pad in Dynamics CRM /365 which will support only in Desktop version…

    1 Comment

Others also viewed

Explore content categories