Converting JavaScript for a Dynamics CRM 2011 Upgrade

Roshan Mehta, 19 January 2012

Upgrading from Dynamics CRM 4.0 to Dynamics CRM 2011 can be a big job especially when the original system is heavily customized. The resulting system after the upgrade needs to be thoroughly tested to ensure that any additional business logic functions as expected. In this post, we will take a look at some of the gotchas involved when converting CRM 4.0 JavaScript into CRM 2011 compatible code.

Converting JavaScript for a Dynamics CRM 2011 Upgrade*

 

Working with Form Fields

CRM 4.0 uses crmForm.all.<attributeName> to retrieve form fields for an entity. This allows us to get/set field values, set focus on the selected field, set the requirement level, disable a field etc. In CRM 2011, we need to use either Xrm.Page.getAttribute(attributeName) or Xrm.Page.ui.controls.get(attributeName). Each function provides the ability to perform different actions on the field. To find out more about the actions that are available for each function, refer to the Microsoft Dynamics CRM 2011 SDK.

Unsupported Customizations

We have recently performed an upgrade on a heavily customized CRM 4.0 system with over 16,000 lines of JavaScript and a lot of unsupported customizations. Anything that addresses the document object model (DOM) is classified as being unsupported and it is possible that any update rollups applied to the system can break these customizations.

Some examples of unsupported customizations include the use of document.getElementById to hide ribbon buttons, form navigation items, or form fields/sections/tabs. Dynamics CRM 2011 provides supported methods to handle each of these scenarios. Ribbon buttons can be hidden or disabled by modifying the ribbon definition XML, form navigation items can be added or removed using the form editor or by using JavaScript, and form fields/sections/tabs can be hidden by setting properties through the form editor.

Another example of an unsupported customization is the use of the window object. This is commonly used to close CRM forms using the window.close() function and can be replaced using the supported Xrm.Page.ui.close() function.

Lookup Filtering

We also found many entities which used JavaScript to modify FetchXML queries for lookups in order to apply filtering using unsupported methods. CRM 2011 offers the ability to filter lookups out-of-the-box. For example, you might have an “Account” and “Primary Contact” lookup on an entity form so that the “Primary Contact” lookup will only show contacts from the selected account. This type of lookup filtering can be achieved through the form editor.

 Converting JavaScript for a Dynamics CRM 2011 Upgrade

For any complex filtering, we can specify a FetchXML query and dynamically display a custom view in the lookup window. This involves using the addCustomView function which allows us to specify the FetchXML query, the name of the view, and whether or not we want it to be displayed by default.

There are tools available which can aid in converting CRM 4.0 JavaScript to CRM 2011 compatible code. I recommend you don’t entirely rely on these conversion tools and that you review all JavaScript to use supported methods and improve performance.

* Image from http://www.affordablehousinginstitute.org/blogs/us/unsupported_small.jpg