Back to Basics # 51: Retrieve Data Using XrmWebAPI with Webresource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, for certain requirements data needs to be retrieved to achieve business functionality with Xrm Web API.  As an example, selected contact record guid was passed to fetch details of 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 which expects few arguments entity name,guid of contact, mention column names which will have couple of parts where we can get data if at all we receive success response then it can be captured in success function part and if there are any errors then it can be captured in error part of function error by using the below code as

Xrm.WebApi.retrieveRecord(“contact”, contactGUID, “?$select=firstname,lastname”).then(

            function success(result) {

                console.log(“Retrieved values: First Name: ” + result.firstname + “, Last Name: ” + result.lastname);

                // 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

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=firstname,lastname”).then(

            function success(result) {

                console.log(“Retrieved values: First Name: ” + result.firstname + “, Last Name: ” + result.lastname);

                // 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. Xrm WebAPI method was widely used while fetching data in Dynamics CRM.
  3. Microsoft documentation can be found here.

Conclusion: In this way, one can easily fetch contacts data for a selected contact record using Xrm Web API.

One thought on “Back to Basics # 51: Retrieve Data Using XrmWebAPI 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

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