Using formContext in Dynamics 365

Leave a comment

In Dynamics 365, the formContext provides a reference to a form or form item through JavaScript. The formContext is part of executionContext, and replaces Xrm.Page which is deprecated in version 9+ of Dynamics 365. The form context object model looks like:

Let’s go through an example of using the formContext. Let’s add a script to a form when the form loads, for example, a Contact entity form. Let’s add a new web resource:

Add a new JScript:

Now let’s add a function. We won’t do anything except set a variable to the formContext. We will then go into debug mode to see what the executionContext looks like:

Save, publish and let’s wire up this code to the OnLoad of the Contact. Be sure to check “Pass execution context as first parameter”:

It should look like:

Click OK, save and publish. Now let’s go to the Contact page and go into your browser’s debugger. Open the new_ContactFormLoad.js and set a breakpoint after the formContext is populated:

We can see the formContext and executionContext have been populated:

Drilling into this, we can see data such as full name:

We can now run formContext code. For example, if we want to set the value of the fullname field, we can go to the Console and run:

formContext.data.entity.attributes.getByName(‘fullname’).setValue(“New value”);

Finish loading the form, and the value will be populated:

There are many other things you can do with executionContext and formContext. Check out here for more details: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/clientapi-form-context

 

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

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