Use form context in HTML web resource without parent.Xrm.Page
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 without using Parent.Xrm.Page and this is our topic today.
In this post, I will explain how you can access the form context from the Html web resource without using Parent.Xrm.Page.
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();
Hope this Helps!
This was originally posted here.

Like
Report
*This post is locked for comments