Xrm.WebApi to retrieve records in D365 CE/Model-driven App

Introduction: In this blog, you’ll learn how to use Xrm.WebApi to retrieve records in D365 CE(CRM) or Model-driven Apps.

This blog also explains how to retrieve and set value in lookup column.

Use Case: 

  • In my example, I have a custom table named “Project Checklist” which has Project, Client, and Email columns. Project and Client are Lookup columns.
  • The project is also a custom table that also has a Client and Email column.
  • Whenever the user selects a value in the Project column in the Project checklist table it should retrieve the value of the client and email from the project table and should set its value automatically in the client and email column of the Project Checklist.

Implementation

Syntax:

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

Code:

var checklistFormCustomizations = {

    retrieveClient: function (exeContext) {

        var formContext = exeContext.getFormContext();
        if (formContext.getAttribute("avt_project").getValue() != null && formContext.getAttribute("avt_project").getValue()[0].id != null) {
            projectID = formContext.getAttribute("avt_project").getValue()[0].id;
            try {
                Xrm.WebApi.retrieveRecord("avt_project", projectID, "?$select=_avt_client_value,avt_email").then(
                    function success(data) {

                        checklistFormCustomizations.setColumnValues(data, formContext);
                    },
                    function (error) {
                        Xrm.Utility.alertDialog(error.message);
                    }
                );
            }
            catch (e) {
                Xrm.Utility.alertDialog(e.message);
            }
        }
        else {
            formContext.getAttribute("avt_client").setValue(null);
            formContext.getAttribute("avt_email").setValue(null);
        }

    },

    setColumnValues: function (data, formContext) {
        try {
            //get the values 

            //set value in lookup field
            var clientId = data["_avt_client_value"];
            var name = data["_avt_client_value@OData.Community.Display.V1.FormattedValue"];
            var entityType = data["_avt_client_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            if (clientId != null) {
                var clientData = [];
                clientData[0] = {};
                clientData[0].id = clientId;
                clientData[0].name = name;
                clientData[0].entityType = entityType;

                formContext.getAttribute("avt_client").setValue(clientData);
            }
            else {
                return;
            }

            //set value in email field
            var email = data["avt_email"];
            formContext.getAttribute("avt_email").setValue(email);

        }
        catch (e) {
            Xrm.Utility.alertDialog(e.message);
        }
    }

}

  •  Call the “retrieveClient” function onChange of Project column and onLoad of Project Checklist form.

Code Explanation:

  • I am retrieving the value of the Project column.
  • If the value is not null then I am using Xrm.WebApi.retrieveRecord request to retrieve the value of client and email from the project table.
Xrm.WebApi.retrieveRecord(<table logical name>, recordGuid, "?$select=column names which you want to retrieve")

Example:
Xrm.WebApi.retrieveRecord("avt_project", projectID, "?$select=_avt_client_value,avt_email")
  • Once request is done successfully set the value in client and email field.

Points to be Noted:

  1. Pass lookup column in select as shown below
_lookupColumnName_value

Example:

Xrm.WebApi.retrieveRecord("avt_project", projectID, "?$select=_avt_client_value,avt_email")

Testing:

Reference: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord

Leave a Reply

Your email address will not be published. Required fields are marked *