Get Parent Record ID Using Dynamics 365 CRM Client API

By | October 15, 2020

Introduction

There are a number of client APIs provided by Microsoft that helps developers work seamlessly with D365. One such useful method is getPageContext() in Xrm.Utility that can be used to get page context as an object representing the page. In this blog, we will see how we can use this method.

For an instance, I want to do some tasks on Quick Create Form of Account entity when I create a record of Account from Contact.

I have a function which is called onLoad of Account entity Quick Create Form, where I want to get some details of the parent records like the Contact Record ID, Name, etc. from where the Account record is going to be created and do further tasks.

To achieve the same, I have written a simple JavaScript function as shown below and called it on load of the Account Quick Create Form.

Note: This method is supported only on Unified Interface.

function onLoadAccountForm() {

try {

var pageContext = Xrm.Utility.getPageContext();

var input = pageContext.input;

var entityType = input.createFromEntity.entityType;

var recordId = input.createFromEntity.id;

var recordName = input.createFromEntity.name;

alert(“Entity: ” + entityType + ” recordId: ” + recordId + ” recordName: ” + recordName);

} catch (e) {

alert(e.message);

}

}

This method returns an object with the input property as shown below:

Get Parent Record Id Using D365 Client API

As I have called the function to get the record object on entity form, I am getting the pageType as “entityrecord” as shown in the above screenshot.

So, once the function gets triggered, I can use the Parent Record ID and other details to do my further tasks depending on the same.

We can also use this method to get the current displayed View ID and View Type along with the Entity Name and Page Type. Here, we will get the value for Page Type as “entitylist”.

Conclusion

Using this method we can get the page context to get the view details or records details and perform further tasks in our development.

Leave a Reply

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