Using formContext in Dynamics 365

1 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:

[sourcecode language=”JavaScript”] function DoLoad(executionContext) {
var formContext = executionContext.getFormContext();
}
[/sourcecode]

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

 

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

One Response to Using formContext in Dynamics 365

  1. Hi Carl,

    Thanks for the post. Really helpful. One of the issue i saw was that it opens 2 windows for edit.

    Also how do we have similar feature for the EDIT button.

    Thanks
    Nilesh

Leave a Reply

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