Technology. People. Progress.
Development

Retrieve Dynamics 365 records with the Web API

Use JavaScript and Xrm.WebApi to retrieve records and related lookup data.

Retrieve Dynamics 365 records with the Web API

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.
Screenshot from the DVMSKE tutorial
Scenario — screenshot 1 View full size

Syntax

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

Code example
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:

NameTypeRequiredDescription
entityLogicalNameStringYesThe table logical name of the record you want to retrieve. For example: “account”.
idStringYesGUID of the table record you want to retrieve.
optionsStringNoOData system query options, $select and $expand, to retrieve your data.
?$select=name&$expand=primarycontactid($select=contactid,fullname)
successCallbackFunctionNoA function to call when a record is retrieved. A JSON object with the retrieved properties and values will be passed to the function.
errorCallbackFunctionNoA 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
Code example
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.

How to Register JavaScript on OnChange Event of Form — screenshot 2
How to Register JavaScript on OnChange Event of Form — screenshot 2 View full size

  • Either Create new Web Resource or add JavaScript Function to existing Web Resource.
  • How to Register JavaScript on OnChange Event of Form — screenshot 3
    How to Register JavaScript on OnChange Event of Form — screenshot 3 View full size
  • In the event handler, in control select the “account name” field and event as on change. Click on add.

How to Register JavaScript on OnChange Event of Form — screenshot 4
How to Register JavaScript on OnChange Event of Form — screenshot 4 View full size

  • 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.

How to Register JavaScript on OnChange Event of Form — screenshot 5
How to Register JavaScript on OnChange Event of Form — screenshot 5 View full size

  • Click on save and publish. Wait until publish.

OUTPUT

Screenshot from the DVMSKE tutorial
OUTPUT — screenshot 6 View full size

Need help applying this to your business?

Talk to our team about your systems and next step.

Talk to our team
Open original image