DYNAMICS 365 HOW TO USE FORM CONTEXT IN HTML WEB RESOURCE
Everyone knows that Xrm.Page is deprecated and it is replaced by the executionContext.getFormContext() method, where the execution context is passed
from the form as the first parameter to the function you are calling (reference
link).
In the same above link, it is mentioned that
Although Xrm.Page is deprecated, parent.Xrm.Page will continue to work in case of HTML web resources embedded in forms
as this is the only way to access the form context from the HTML web resource
.
However, you can access the form context from HTML web resource without using Parent.Xrm.Page and this is the topic for today.
You have to create a function that will return the formContext, then, on load of the form call this function, and finally call this function from the web resource.
-
Declaration of a variable and function that return the formContext
var formContext;
function getFromContext() {
return formContext;
}
-
On load of the form, fill the variable by calling the function that returns the formContext
function onLoad(context) {
//Set Form Context
formContext = context.getFormContext();
Xrm.formContext = getFromContext();
}
-
Call the function that returns the formContext from the web resource in the form without using parent.Xrm.Page
var recordId = parent.Xrm.formContext.data.entity.getId();
Bonus Tip:
In case the web resource opens another web resource in a new window, you can still access the formContext as well from the new window using the following syntax:
var recordId = window.top.opener.parent.Xrm.formContext.data.entity.getId();
In case the web resource opens another web resource in a new window, you can still access the formContext as well from the new window using the following syntax:
var recordId = window.top.opener.parent.Xrm.formContext.data.entity.getId();
Hope This Helps!
Hi there - It looks like I could use this to call an external web resource including the account name (or another field). Would you be able to explain how I would put this into practice using your code above please?
ReplyDeleteHello Mick,
DeleteTo call a webresource, you should add it as a web resource file into D365 and call it as follows
var customParams = "Param1=" + YourFieldValue ;
Xrm.Navigation.openWebResource("WebResourceName", null, encodeURIComponent(customParams));