Retrieve records using Fetch XML & Java Script

Goal: Retrieve entity data using FetchXML & Javascript

Process:

Step 1: download Xrm svc Toolkit, and download the Zip folder. Unzip XrmServiceToolkit the folder.

Step 2: Open the XrmServiceToolkit folder, you can find the below Javascript files.

  1. Jquery
  2. Json2
  3. XrmServiceToolkit
  4. XrmServiceToolkit.min

Goto Microsoft Dynamics 365 >> Settings >> Customization >> Webresources.

Upload these files into MSD 365 solution.

Step 3: Add all these 3 files to the target entity’s form,

Step 4: Goto advance find and create new criteria and download the FetchXML

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="telephone1" />
    <attribute name="contactid" />
    <order attribute="fullname" descending="false" />
    <filter type="and">
     <condition attribute="fullname" operator="eq" value="Arun Potti" />
    </filter>
  </entity>
</fetch>

Step 5: We have to change the format of the above FetchXML to use in Javascript, this FeychXML must be converted to JS string. After modifying its looks like the below,

"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>",
"  <entity name='contact'>",
"    <attribute name='fullname' />",
"    <attribute name='telephone1' />",
"    <attribute name='contactid' />",
"    <order attribute='fullname' descending='false' />",
"    <filter type='and'>",
"      <condition attribute='fullname' operator='eq' value='Sample Name' />",
"    </filter>",
"  </entity>",
"</fetch>"

Step 6: upload this JS to MSD with below code

function Onload() {
 var contactFetchXML = "your FetchXML";
var dataRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML);
if (contactRecords.length > 0) {
 if (contactRecords[0].attributes.telephone1 != undefined)
   alert(contactRecords[0].attributes.telephone1.value);
  }
}

Step 8: Add the new_contactJscript Webresource to Form Libraries on contact Form, and add the Onload function to Onload Event,

FetchXML - Pic 5

Step 9: Click on Ok. Save & Publish the target Entity.

Step 10: … and you are done, you are ready to check your functionality.

Thanks!

One thought on “Retrieve records using Fetch XML & Java Script

  1. HIi,

    Can you please assist if i have my link entity in fetchxml then XrmServiceToolkit gives error. Please help. Thanks in advance.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s