Xrm.Page.getControl("webResource1").getObject() is working fine in Sales Dynamics 365, while it's not working in Sales Hub.
The counterpart is to use formContext, but how to call a script in a web resource from another web resource.
Eg. Xrm.Page.getControl("webResource1").getObject().contentWindow.function1() is working fine in Sales, but not in Unified UI.
2 Answers
Xrm.Page has been deprecated (Deprecated methods are here)
You now have to do the following:
- When you register your event you must tick the
Pass Execution Context as first parametercheckbox - In your function you have to add a new parameter called
executionContext(the name doesn't really matter)
When you've done the above, you can access the new formContext object which contains most of the methods included in Xrm.Page
More information on formContext here
For example:
function myHandler(executionContext) { var formContext = executionContext.getFormContext(); var myWebResource = formContext.getControl("webResource1"); } Edit to address null getObject error
The object returned from getControl() does have a getObject method (MSDN) which states it will return either:
- An IFRAME returns the IFrame element from the Document Object Model (DOM).
- A Silverlight web resource will return the Object element from the DOM that represents the embedded Silverlight plug-in.
If you're getting a null object then you've probably got an error with the name of your webresource.
Open your CRM form in designer mode and and locate your webresource. Double-click to view its properties. You're looking for the field name which in CRM Online is prefixed with "WebResource_"
For example:
2There’s another design change causing this null when using formContext.getControl("WebResourceName").getObject() because Sales hub or UCI form tab having web resource is not rendered yet unless it’s clicked/navigated.
Similar topic in Dynamics community thread
