!

Some useful Javascript code samples for Microsoft CRM 2011 [ for newbies]


Some useful Javascript code samples for Microsoft CRM 2011 [ for newbies]

I recently came across a situation where I had to customize the forms of Microsoft CRM 2011 application. Microsoft Dynamics CRM enables developers with its strong JavaScript library, using which we can  customize CRM web application as per customer requirement and business logic.


In this article I am giving a list of very basic javaScript code sampls which will help new comers to get a hang of form customization using JavaScript.

The image below shows the layout of JavaScript stack for Microsoft Dynamics CRM 2011

JavaScript stack for Microsoft Dynamics CRM 2011 form customization web application


Get the value from a CRM field

var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ;

Set the value of a CRM field

Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);

Hide/Show a tab/section

Xrm.Page.ui.tabs.get(5).SetVisible(false);
Xrm.Page.ui.tabs.get(5).SetVisible(true);

Call the onchange event of a field

Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();

Get the selected value of picklist

Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;

Set the requirement level

Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”);

Set the focus to a field

Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);

Stop an on save event

event.returnValue = false;

Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()



Thanks for reading! Enjoy!

Feel free to stop and write your comments below.