Sunday, September 30, 2012

Useful Java Scripts for CRM 2011


This is the first post of my blog. Hope to be useful! Here, we are discussing about adding Jscript to forms and fields.

To add script to a form follow these steps:
1- On the form, click on "Form Properties".
2- In the "Events" tab, add a new library.
3- Create a new library, and type a name and select the type as "Script". After you have saved, you can enter the script in "Text editor". At the end of this article you will find some examples.
4- At the last step, you have to add the handlers. In Dynamics CRM 2011, the forms have two type: "On load", "On save". Here, you should fill the library name and function. The function name is the same as in the script.
To add script to a filed follow these steps:
1- On the form, double click on the field.
2- Find the events tab.
3- Add required libraries for the field.
4- The fields in Dynamics CRM 2011 only has one type of handler: "On change".
After you have added and published the changes, the scripts should work! Of course, if it is true!

Some useful examples for CRM 2011:

1- Retrieving the value of a field:

Xrm.Page.getAttribute("schema name").getValue()

2- Setting value for the variables:

Xrm.Page.getAttribute("schema name").setValue()

3- Disabling a field:
Xrm.Page.ui.controls.get("schema name").setDisabled(true)

4- Disabling the options of an option set:

Xrm.Page.getControl("schema name").removeOption(option value)

5- Hiding a ribbon button:

var x=top.document.getElementById("entity name|NoRelationship|Form|button name");'none';

6- Hiding a filed on the form:
Xrm.Page.getAttribute("schema name").setVisible(false)