Javascript Best Practices
While Customizing CRM in client side you should follow the below best practices.
- Do not directly edit scripts in script editor window of CRM. Always use Visual studio to edit so that you can figure out the syntax errors properly.
- Limit the number of Script libraries your form references. Performance can lag when you view a form for the first time that has multiple scripts. Starting with CRM 2013, scripts are loaded asynchronously and any onload event will fire as soon as the scripts are all downloaded. Additionally, if your OnChange or OnSave events require certain libraries, do not load them with the OnLoad event because of the perceived performance lag.
- Remember to collapse all the tabs when you design your forms so that the page can load as fast as possible. All elements inside the sections of the collapsed tabs are not loaded until the tab is opened. There is a TabStateChange event that can invoke your code as needed.
- write clean javascript codes with proper comments. Watch this video to understand how to use javascript in CRM.
- For cheetsheet for javascript reference card you can download here.
- Keep it simple
- Use safe and efficient logic
- Use non-block queries
- Use read-only, unique values
-
Avoid unsupported code
Below JavaScript features and functions that are unsupported in CRM 2011 are:
- .getElementById() – Although this method may not fail, it is commonly used for DOM manipulation which is not supported in CRM. As best practice it should be avoided.
- JQuery – This powerful JavaScript library provides helper methods that allow developers to do just about anything they want with JavaScript with a very small footprint. Although it is not officially supported, versions of JQuery are shipped with the Dynamics CRM 2011 SDK The mechanics behind its powerful methods may carry out unsupported actions and therefore result in errors. Use with caution.
- crmForm Object Model – This syntax was the official object model of CRM4.0 but is no longer supported with CRM 2011 Update Rollup 12 and newer. Methods using this syntax are prone to failure in these versions and can fail without producing error messages.
- .FireOnChange() – Like the crmForm object model, this method has been depreciated. The .fireOnChange() method (with a lower case ‘f’) is available through the Xrm.Page context and should be used instead.
Follow this link for more info.
You can also get more info about script customization here.