Some CRM implementations require the use of custom Jscript to pop open a new entity form window or a window displaying an existing record in CRM. Previously, there was no “supported” way to do this, since we had to use the window.open Jscript object which relies on the use of the Document Object Model (DOM). Update rollup 8 provides new SDK functions which CRM configurators can use to open CRM forms and web resources in a nice supported way.
The Unsupported Way
Entity forms are URL Addressable. This means we can build a URL and include query string parameters to identify the record type and ID of the record we want to open in CRM. We would still need to use the unsupported window.open method in our Jscript web resource to actually pop open the record.
We could also pop open Web Resources (e.g. HTML/Silverlight) using Jscript. Each Web Resource is also URL Addressable, however it can be tricky at times to work out the correct URL to use, especially when we’re dealing with relative URLs.
Here is an example of how to pop open an existing Account using Jscript:
function popAccount() {
var accountId = "8FB2CCE4-20E4-E111-9700-00155D042F0D";
var url = Xrm.Page.context.getServerUrl() + "/main.aspx?etn=account&id=" + accountId + "&pagetype=entityrecord";
window.open(url);
}
Here is an example of how to pop open an HTML Web Resource using Jscript:
function popHTML() {
var url = Xrm.Page.context.getServerUrl() + "//WebResources/ros_/html/roshan.html";
window.open(url);
}
Although these methods do work, they could potentially break as new rollups are applied to your system, or when CRM 2011 is available on multiple browsers.
The Supported Way
Update Rollup 8 includes a couple of new Jscript SDK functions as part of the Xrm.Utility namespace. These are Xrm.Utility.openEntityForm and Xrm.Utility.openWebResource
Firstly, let’s take a look at Xrm.Utility.openEntityForm. This function takes three parameters – the schema name of the entity, the unique identifier of the record, and a set of parameters we want to pass to control things like the window size and position. Here is an example of how we can open up the exact same Account record as I have shown in the unsupported method above:
function popAccount() {
var accountId = "8FB2CCE4-20E4-E111-9700-00155D042F0D";
Xrm.Utility.openEntityForm("account", accountId);
}
If we wanted to pop open a new Account form, we can exclude the ID parameter.
Here is an example of how to pop open an HTML Web Resource using Jscript:
function popWebResource() {
var webResourceName = "ros_/html/roshan.html";
Xrm.Utility.openWebResource(webResourceName);
}
Thanks to these new functions, we can maintain clean and supported code for our CRM implementations. I have only shown you the tip of the iceberg! In my next post, I will show you how we can use the Xrm.Utility functions to control window size and position as well as pass parameters to a form or web resource.