Using FormContext as Global variable in D365

Hello Guys,

This blog will explain the use of Form Context or Execution Context as Global variable in D365.

We know that Client Side Scripting using Java Script is one of the best way to handle business process logic whether it is a client side validation or displaying data on model driven app.

For implementing this functionalities one of the main feature is Xrm.Page which is deprecated now and we have to use Execution Context instead of this. To use Execution Context we need to pass it as parameter in Java script function as shown in below image.


Consider a scenario where we are having multiple functions which is using Execution Context on single entity. Now to implement this either we can pass execution context as first parameter in all function as shown above image or we call function by passing parameter in it. This approach is fine when we have less functions but what about where we are working on large functions used in a single entity.

In this scenario we can use Form Context or Execution Context as global variable.
Also there are some scenario where we need to use Primary Control while working with ribbon workbench. Here we need to pass Primary Control CRM Parameter.

We can handle both the scenario as single approach and that is use Form Context as global variable. You can implement below steps to achieve this.

Step 1: Create one Java Script file in Visual Studio or in any editor which you prefer. And Paste below code:


Step 2: Upload this Java Script as web resource in D365.


Step 3: Select Entity on which you want to add this function. For example, In below image we have used contact entity. Open Form Editor and Click on Form Properties. Click on ADD button in Form Libraries Section to add Web Resource. Once Web resource is added then select Form Control and select it's OnLoad event and click on ADD button.


Step 4: Select library and specify the function name and check the check box that is Pass execution context as first parameter and click on OK. Publish the customization.



Step 5: Now you can use FormContext variable in other functions too. You don't need to pass it as first parameter again. Also you can use the same variable while working with Ribbon workbench as you don't need to pass primary Control as CRM Parameter.

Hope it helps...














Comments

Post a Comment

Popular posts from this blog

Read Only Sub Grid & Editable Sub grid in Dynamics 365

Understanding Sales Process in D365.

Understanding Business Unit in Dynamics 365.