Please wait, loading...

 

Call Xrm.WebApi.retrieve Record using JavaScript MSCRM/CE , Retrieving lookup / related entity using Web API JavaScript FetchXML Microsoft Dynamics CE / CRM

June 9, 2023

Retrieve a record in Dynamics 365 Online V 9.X using JavaScript WebAPI

In Dynamics 365 9.x, We can use the new Xrm.WebApi JavaScript method to call the Dynamics 365 Web API.These new methods are very useful in that developers can easily create, get, update, delete, etc. with just a few lines of code

Scenario

  • We have a requirement to retrieve an entity record from a lookup (Account Lookup) On Contact entity.
  • On Change of Account Lookup, JavaScript will execute Xrm.WebApi.retrieve on Contact Entity and get fax field from Account and set in fax field on contact.
  • JavaScript (Event Handler) will be added on Contact entity and OnChange Event of Account Lookup. So JavaScript function will trigger on change of account Lookup.
https://dvmske.com/wp-content/uploads/2023/06/Retrieve-a-record-in-Dynamics-365-Online-V-9.X-using-JavaScript-WebAPI-_-DVMSKE_1.jpg

Syntax

Below is the latest syntax available for retrieving records in Dynamics 365 Online V9.X using JavaScript.

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

Example :

Xrm.WebApi.retrieveRecord("account",lookupcustomerId , "?$select=fax&$expand=parentaccountid($select=accountid,name)").then(function success(result){},function(error) { }

Parameters:

Name Type Required Description
entityLogicalName String Yes The table logical name of the record you want to retrieve. For example: “account”.
id String Yes GUID of the table record you want to retrieve.
options String No OData system query options, $select and $expand, to retrieve your data.
?$select=name&$expand=primarycontactid($select=contactid,fullname)
successCallback Function No A function to call when a record is retrieved. A JSON object with the retrieved properties and values will be passed to the function.
errorCallback Function No A function to call when the operation fails.

Xrm.WebApi.retrieveRecord Example

Below is the JavaScript function “onChangeOfAccountNameOnContact

  • 1st create form context from execution context passed as 1st parameter
  • Get the Account lookup GUID, if field is not null
  • use the GUID and Account entity schema name to Get Record in selected in Lookup
  • Getting Fax field value from result
  • Uses the retrieved value to set in Fax field of account
function onChangeOfAccountNameOnContact(executionContext)
{
    debugger;
    var formContext = executionContext.getFormContext();
    if (formContext.getAttribute("parentcustomerid").getValue() != null &&
    formContext.getAttribute("parentcustomerid").getValue()[0].id != null)
    {
        var lookupcustomerId = formContext.getAttribute("parentcustomerid").getValue()[0].id;
        Xrm.WebApi.retrieveRecord("account",lookupcustomerId , "?$select=fax&$expand=parentaccountid($select=accountid,name)").then(
            function success(result)
            {
                debugger;
                if(result!=null)
                {
                    var faxValue=result.fax;
                    formContext.getAttribute("fax").setValue(faxValue);
                }
            },
            function(error) {
                alert(error.message);        
     }
        );
    }
}

How to Register JavaScript on OnChange Event of Form

  • Create a New Solution and Name it appropriately, In your solution, add contact entity then select  contact form.
  • From component section select Contact entity added and open Form editor
  • From From editor , click on Form Properties.

  • Either Create new Web Resource or add JavaScript Function to existing Web Resource.
  • In the event handler, in control select the “account name” field and event as on change. Click on add.

  • In the library select the library where you add your code. And in function copy your js function. And check both the checkboxes. Click on ok.

  • Click on save and publish. Wait until publish.

OUTPUT

https://dvmske.com/wp-content/uploads/2023/06/Call-Xrm_WebApi_retrieve-Record-using-JavaScript-MSCRM-DVMKE.jpg

anurag Tyagi