Best Way to Write JavaScript in Dynamic CRM

We are very friendly with JavaScript and commonly use it in dynamics like onLoad() events for hiding some fields, applying some color to text , doing some calculation etc and onChange() events for assigning the recode, showing some fields etc.

Commonly used Steps to add JavaScript library in Dynamics are:

1. Go to form property, click on add button,


2. Add new web resource and give the name of web resource. Click on text editor {to write JavaScript },


3. For example, we want to fill the textbox on load and save recode on change of Payment_Status filed. So we create one function auto_save.

A. FillData() : On form load some text will show in assign filed.

B. Auto_save() : On change of “XXXX” filed recode will save.


4. Web resource added.

 

5. Now we need to add our both function.

A. Filldata() : on load event.


B. AutoSave() : on change event, here we attach our auto_save function to “Payment Status” filed.


Note: 

On Change event is attached on some specific field. So we need to specify the field name and mention the function names. In large projects we have so many fields and it's very difficult to identify which function is attachde with which filed.

To reduce this overhead we need a little bit of change in our coding style.

1. Add two functions in our script.


2. Need to add only OnLoad event. And all controls regarding to Onchange event write in script in attachEvents(). From this way of code increase the readability.