Common Javascript Functions For CRM 2011

Get the value from a CRM field

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

Set the value of a CRM field

Xrm.Page.getAttribute(“CRMFieldSchemaName “).setValue(“New Value”);

Get the value from a CRM OptionSet field

var value = Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getValue();

Get the text from a CRM OptionSet field

var text = Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getText();

Set the value of a CRM OptionSet field

Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).setValue(“1″); // OptionSet Value

Get the selected text of a CRM OptionSet field


Get the selected value of a CRM OptionSet field


Get the text and value of a CRM Lookup field

var lookupObject = Xrm.Page.getAttribute(“CRMLookupSchemaName”).getValue();

lookupObject[0].name; // text of lookup

lookupObject[0].id; // Guid of lookup

Set the value of a CRM Lookup field

var lookupData = new Array();

var lookupItem = new Object(); = “4A2A54CB-349C-E111-8D26-1CC1DEE8DA78″; // Guid of record = “New Contact”; // Entity record name

lookupItem.entityType = “EntitySchemaName”;

lookupData[0] = lookupItem;


Disable CRM field


Hide CRM field


Hide a Tab in CRM


Hide a Section in CRM

var tab = Xrm.Page.ui.tabs.get(“tabName”);


Set the Requirement level in CRM




Set Focus on a field in CRM


Cancelling Onsave Event in CRM

event.returnValue = false;

return false;

Check IsDirty in CRM field

var isDirty = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getIsDirty();

alert(isDirty); // returns true if the field is dirty

Check IsDirty for all the fields in CRM

var isDirty =;

alert(isDirty); // returns true if any of the field is dirty in the entire form.

Force Submit a read only field in CRM


Preventing an attribute to be saved in CRM form


Get Unique Organization Name in CRM


Get Server url in CRM


Get the record Id in CRM;

Get the User Id in CRM


Get the Entity Schema Name in CRM;

Get the UserRole Id’s in CRM

var userRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < userRoles.length; i++)


var userRole = userRoles[i]; // returns the Role Id


Get the Form Type in CRM


Form Types in CRM

Is the user creating a new record?

Xrm.Page.ui.getFormType() == “1″

Is the user updating an existing record?

Xrm.Page.ui.getFormType() == “2″

Is the user unable to update this record?

Xrm.Page.ui.getFormType() == “3″

Is this record deactivated?

Xrm.Page.ui.getFormType() == “4″

Is the user using the Quick Create form?

Xrm.Page.ui.getFormType() == “5″

Is the user using the Bulk Edit form?

Xrm.Page.ui.getFormType() == “6″

Save a record in CRM; // for saving a record“saveandclose”); // for save and close“saveandnew”); // for save and new

Close the form in CRM