In JavaScript we can only fetch attributes of the entity on which we create the event. Usually, we come across scenarios where we have the relationship between two entities and from child entity record, we try to fetch parent entity record and update values.

E.g., while creating a phone call record in Dynamics 365 we have a regarding field which is related as a parent entity to phone call. Now let’s assume we are creating phone call for a lead and trying to capture its status on creation on phone call form.

In order to achieve, we have to install “CRM Rest Builder” solution in the current environment.

Use the following link to download CRM Rest Builder Solution:

GitHub – jlattimer/CRMRESTBuilder: A tool for CRM 2015/2016 & D365 CE (7.0+) Online & On Premise to generate JavaScript code using the 2011 & Web API REST endpoints.

First, we must check if phone call regarding has a value or not.

if (formContext.getAttribute(“regardingobjectid”) != null && formContext.getAttribute(“regardingobjectid”).getValue() != null && formContext.getAttribute(“regardingobjectid”).getValue().length > 0)

Then we have to check if regarding entity is lead nor not.

var regarding = formContext.getAttribute(“regardingobjectid”).getValue()[0].entityType;
if (regarding == “lead”)

We have to fetch form type and check if it is a create or not.

var formType = formContext.ui.getFormType();
if (formType == 1)

We have to get the GUID of the regarding lead.

var regardingId = formContext.getAttribute(“regardingobjectid”).getValue()[0].id;
var newid = regardingId.slice(1, -1);

Now with lead GUID we need to fetch status field from lead and then set it on to phone call form.
For this open solution in D365, after installing CRM Rest Builder we’ll have an option to open it on top left corner.

Based on requirement we’ll select options available as:

Endpoint: Web API 9.1 v9.2.21091.126

Action: Retrieve Single

Output Format: XMLHTTP

Formatted Values: Yes

Detect Changes: No

Token Header: No

Impersonate: No

Process: Asynchronous

Primary Entity: <Select Lead>

Id to Retrieve: <Provide random GUID for time being>

Field to Select: <Select Status>

Then click on “Create Request”

Copy code and use it in JavaScript.
In order to update static GUID with regarding phone call GUID, we have already defined a variable “newid” to store the“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v9.1/leads(” + newid + “)?$select=statecode”, true);

In success condition, if(this.statue === 200)

Write set value statement to set value of status record on phone call.


With this we have picked GUID of a record and fetched its attributes and updated it in current record.

Leave a Reply