JavaScript best Practices for CRM

JavaScript can be used with CRM to perform actions in form scripts, command bar commands, and in web resources. While we extensively use JavaScript for the above mentioned actions we need to make sure that some of the best practices are followed.

Avoid using unsupported methods

From different suggestions from internet we will implement functions which are not supported but working as expected for the time being. These actions are not recommended by Microsoft. Its always better to use only supported methods to make sure that our code works even tomorrow. There is no guarantee that our unsupported methods will work in a future release as Microsoft will not take care of any non recommended methods. They don’t have to. Better avoid using unsupported stuff when we use JavaScript methods.

Use a cross browser library

Always a cross browser library like jQuery will help the developers in developing web resources in CRM. Keep in mind to use these resources only with the HTML resources and avoid using unsupported methods to access CRM related objects.

Do not use jQuery for form scripts or commands

Continuation of the above point take a decision yourself to decide where to use jQuery.

Recognize limitations for content delivery network libraries

It need to be made sure that we are using CDN libraries very less at least understand the limitations. As we know CRM for outlook can be used offline as well. At that point of time CDN libraries won’t work as expected. Internet access is mandatory to work with CDNs perfectly which may not be the case always. So using them is always a risk to make sure they will work always. An alternative is to use web resources for javascript which will be synchronized when the outlook stuff goes online, even if any organization restricts internet access these web resource will be part of the organization and will never be blocked.

Use feature detection when writing functions for multiple browsers

When we target for multiple browsers, normally what we does is identifying the browsers and move ahead accordingly. But that never gives us a clear picture on what all features that particular browser supports. So its always good to detect the features instead of detecting the browser. How to detect features not browsers?

Do not access DOM

Always try to access CRM application pages or entity forms using Xrm.Page object model, we are tend to access these via DOM commands. These are always good to used only in a web resource. But wherever Xrm.Page object model can be used better to take that route as the DOM route is not supported by MSFT in this context. MSFT reserves the right to change how the pages are built and the elements in the pages and so on. So its always a risk to access and the elements via DOM.

Define unique names for JavaScript functions

There can be multiple resources being added to a CRM page. If two javascript functions on a page have the same name, the first function defined is overwritten by the second. For this reason its always safe to make sure that the names are unique for functions.

Use asynchronous data access methods

To make sure the browser is responsive, its always recommended to run long running processes like data access using SOAP and REST endpoint asynchronously. Always use an XMLHttpRequest that is configured to execute asynchronously. This is because the browser will be running in a single thread and long running data processes can make is non responsive.

One thought on “JavaScript best Practices for CRM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s