JavaScript with Dynamics CRM – 1

Posted by

From my this article, now we are familiar with the environment set up. In this article, I am going to cover Best Practices for working with JavaScript in Dynamics CRM.

Naming Convention –

  • Name JavaScript files, specific to your entity and form components.
    • If my entity is client and my JavaScript has many functionalities then I will name it as – Client_main.js
    • If my main JavaScript file has too many functionalities, then for Quick form I will create a new JavaScript file as – Client_quick.js
    • On the same line, I can create another JavaScript resource for others forms as well.
  • Try to keep variable and function name short and meaningful.
  • Make the naming convention consistent. Use either Pascal or Came case, but be consistent with it.

 

No. of Javascript files

In a form, you can have up to 50 js files but it doesn’t mean you should have 50 small files in a form. try to keep that count minimum.

Capture

Refer Attribute Name –

It always becomes confusing, what name we should use in our JavaScript code. Always remember to use Name as shown here for the attribute name. For more detail of different names and their usage, please read my this article.

Untitled

What NOT to use –

  • Do not use getElementById or any other DOM manipulation techniques. They are NOT supported and may not give the expected result. Instead, use Xrm.Page Object model.
  • Avoid from having multiple Javascript files, as they are downloaded into your browser and may slow down your page loading.
  • Do Not expand all the tabs on form load. Tab loads its resource when it is expanded for the first time. Instead, keep them collapsed and expand only those are mandatory.
  • Do NOT use parenthesis for the function name. like this Untitled1

Few more Important Points –

  • All Bussiness rules are executed after JavaScript files.
  • If you have code that only supports OnChange events for fields or the OnSave event, make sure to set the script library with the event handler for those events instead of the OnLoad event. This way loading those libraries can be deferred and increase performance when the form loads.
  • Avoid using form scripts in the OnLoad event that hide form elements. Instead, set the default visibility options for form elements that might be hidden to not be visible by default when the form loads. Then, use scripts in the OnLoad event to show those form elements you want to display.

 

I hope this helps. These are based on my experience and below links.

 

Happy Dynamics’ing !!!

 

References –

 

 

 

 

One comment

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 )

Google+ photo

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

Connecting to %s