Form Context in Dynamics 365 version 9

7.4k Views Asked by At

I need to access a control of a web resource to refresh it. According to the link below we need to have the form context.

But I am unable to get the form context and couldn't find it anywhere in the documentation on how to access the execution context in the HTML web resources. I have tried registering a function on load of the form and passing execution parameter in it using form properties. Then in the function I am getting it like below with formContext variable as a global variable.

var formContext;
function getFormExecutionContext(executionContext) {
    formContext =  executionContext.getFormContext();  
    console.log("Form Context: "); 
    console.dir(formContext); 
}

However when I tried to access this formContext in other HTML web resource placed on CRM form, it says undefined. Can someone please explain how can we get the form context in HTML web resource?

2

There are 2 best solutions below

2
On

Normally we will include the below snippet in HTML webresource head section, this will present you the CRM context & controls outside CRM form.

<head>
    <title>HTML Web Resource</title>
    <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
</head>

Then access the controls like following:

parent.Xrm.Page.getAttribute("my_control").getValue();

The same should work in v9 as well for backward compatibility, may be not mentioned in documentation.

0
On

In case your ribbon button is on subgrid and there you need to access FormContext in version 9.0 or after, here is the detail.

In Ribbon part, pass the below parameter.

<Actions>
    <JavaScriptFunction FunctionName="subgridEvent" Library="$webresource:new_contactformload.js"> 
        <CrmParameter Value="PrimaryControl" /> 
     </JavaScriptFunction>
</Actions>

And here is the function to access the Form Context.

function ribbonHandler(e) { 
    var formContext = e.getFormContext();
    var recordId = formContext.data.entity.getId(); 
    var fieldValue = formContext.getAttribute("<field_name>").getValue(); 
}

Here is the Reference which saved my time.