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.
  1. Declaration of a variable and function that return the formContext

    var formContext;
    function getFromContext() {
    return formContext;
    }


  2. 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();
    }


  3. 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!

Comments

  1. 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?

    ReplyDelete
    Replies
    1. Hello Mick,
      To 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));

      Delete

Post a Comment

Popular posts from this blog

DYNAMICS 365 FILTER LOOKUP FIELD BASED ON N:N RELATIONSHIP

DYNAMICS 365 LEVEL UP BROWSER EXTENSION - PART 1 - FORMS

DYNAMICS 365 REFERENCE PANEL