Saturday, 27 April 2013

CRM 2011 Basic Javacript Syntax



Set the value of a CRM field
Xrm.Page.getAttribute(“CRMFieldName“).setValue(“New Value”);


Get the value from a CRM field
var value = Xrm.Page.getAttribute(“CRMFieldName”).getValue();

Get the value from a CRM OptionSet field
var value = Xrm.Page.getAttribute(“CRMFieldName”).getValue();

Get the text from a CRM OptionSet field
var text = Xrm.Page.getAttribute(“CRMFieldName”).getText();

Set the value of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMFieldName”).setValue(“1″); // OptionSet Value

Get the selected text of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMFieldName”).getSelectedOption().text;

Get the selected value of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMFieldName”).getSelectedOption().value;

Get the text and value of a CRM Lookup 
var lookupObject = Xrm.Page.getAttribute(“CRMLookupName”).getValue();
lookupObject[0].name; 
lookupObject[0].id;

Set the value of a CRM Lookup field
var lookup = new Array();
var Item= new Object();
Item.id = "Guid Value"
Item.name = “Contact”; // Entity name
Item.entityType = “EntityName”;
lookup [0] = Item;
Xrm.Page.getAttribute(“CRMLookupName”).setValue(lookupData);

Disable CRM field
Xrm.Page.ui.controls.get(“CRMFieldName”).setDisabled(true);

Hide CRM field
Xrm.Page.ui.controls.get(“CRMFieldSchemaName”).setVisible(false);

Hide a Tab in CRM
Xrm.Page.ui.tabs.get(“tabName”).setVisible(false);

Hide a Section in CRM
var tab = Xrm.Page.ui.tabs.get(“tabName”);
tab.sections.get(“sectionName”).setVisible(false);

Set the Requirement level in CRM
Xrm.Page.getAttribute(“CRMFieldName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldName”).setRequiredLevel(“recommended”);

Set Focus on a field in CRM
Xrm.Page.ui.controls.get(“CRMFieldName”).setFocus(true);

Cancelling Onsave Event in CRM
event.returnValue = false;
return false;

Check IsDirty in CRM field
var isDirty = Xrm.Page.getAttribute(“CRMFieldName”).getIsDirty();
alert(isDirty); // returns true if the field is dirty

Check IsDirty for all the fields in CRM
var isDirty = Xrm.Page.data.entity.getIsDirty();
alert(isDirty); // returns true if any of the field is dirty in the entire form.

Force Submit a read only field in CRM
Xrm.Page.getAttribute(“CRMFieldName”).setSubmitMode(“always”);

Preventing an attribute to be saved in CRM form
Xrm.Page.getAttribute(“CRMFieldName”).setSubmitMode(“never”);

Get Unique Organization Name in CRM
Xrm.Page.context.getOrgUniqueName();

Get Server url in CRM
Xrm.Page.context.getServerUrl();

Get the record Id in CRM
Xrm.Page.data.entity.getId();

Get the User Id in CRM
Xrm.Page.context.getUserId();

Get the Entity Schema Name in CRM
Xrm.Page.data.entity.getEntityName();

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
Xrm.Page.ui.getFormType();

Save a record in CRM
Xrm.Page.data.entity.save(); // for saving a record
Xrm.Page.data.entity.save(“saveandclose”); // for save and close
Xrm.Page.data.entity.save(“saveandnew”); // for save and new

Close the form in CRM
Xrm.Page.ui.close();

No comments:

Post a Comment