Back to Basics # 52: Retrieve Related entities Data Using Expand Query with Webresource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, for certain requirements data needs to be retrieved from related entities. Expand query in XRM Web API.  As an example, selected contact record guid was passed to fetch details of account that is related to the given contact .

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, in JavaScript file(webresource) use the below code to fetch the contact GUID for a selected record

let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

as shown in the below figure

Step 4:

After Step 3, we can use Xrm.WebApi with retrieve Record method frame expand query which expects account (lookup) related column that is present in contact entity with  the below code as

  Xrm.WebApi.retrieveRecord(“contact”, contactGUID, “?$select=fullname,telephone1,contactid&$expand=parentcustomerid_account($select=name)&$filter=(parentcustomerid_account/accountid ne null)”).then(

            function success(result) {

                console.log(“Retrieved values of Account : Account Name: ” + result.parentcustomerid_account.name);

                // perform operations on record retrieval

            },

            function (error) {

                console.log(error.message);

                // handle error conditions

            }

        );

As shown in the below figure

Step 5:

After Step 4,  summing it together the final code will looks like below which needs to be called from respective on load function

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        getContacts(executionContext);

        },

    __namespace: true

}

function  getContacts(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

        Xrm.WebApi.retrieveRecord(“contact”, contactGUID, “?$select=fullname,telephone1,contactid&$expand=parentcustomerid_account($select=name)&$filter=(parentcustomerid_account/accountid ne null)”).then(

            function success(result) {

                console.log(“Retrieved values of Account : Account Name: ” + result.parentcustomerid_account.name);

                // perform operations on record retrieval

            },

            function (error) {

                console.log(error.message);

                // handle error conditions

            }

        );

    }

}

As shown in the figure

Step 6:

After Step 5,  save and publish Webresource which was registered on onload function of Contact Form as shown in the below figure.

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. It is quicker to frame this Expand statement using xrm toolbox Fetchxmlbuilder by selecting equivalent odata 4.0 option.
  3. Microsoft documentation can be found here

Conclusion: In this way, one can easily retrieve related entities details using expand keyword in XRM Web API Query using web resource.

4 thoughts on “Back to Basics # 52: Retrieve Related entities Data Using Expand Query with Webresource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind April 2022 – Common Man Tips for Power Platform, Dynamics CRM,Azure

  3. Pingback: Rewind April 2022 - Common Man Tips for Power Platform, Dynamics CRM,Azure - Microsoft Dynamics CRM - Microsoft Dynamics Community

  4. Pingback: Rewind April 2022 - Microsoft Dynamics CRM Community

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 )

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