Dynamics CRM 2011 · Javascript

CRM 2011 Useful JavaScript tidbits

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

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

Hide/Show a section

Xrm.Page.ui.tabs.get(5).sections.get(0).setVisible(false);
Xrm.Page.ui.tabs.get(5).sections.get(0).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;
executionObj.getEventArgs().preventDefault();

Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()

Original link here.

Get record GUID

var recordId = Xrm.Page.data.entity.getId()l
var recordIdWithoutCurlyBraces = Xrm.Page.data.entity.getId().substring(1,37);

Get event source

var eventSource = executionContext.getEventSource(); // tick Pass execution context as first parameter

Get event source (fieldname)

var fieldName = executionContext.getEventSource().getName(); // tick Pass execution context as first parameter

Get event source (value)

var fieldValue= executionContext.getEventSource().getValue(); // tick Pass execution context as first parameter

Set Lookup Value

Xrm.Page.getAttribute('new_fieldid').setValue([{ id: 'guid', name: fullName, entityType: 'entityTypeName'}]);

Original link here.

Disable/Enable field

Xrm.Page.getControl(fieldName).setDisabled(true);
Xrm.Page.getControl(fieldName).setDisabled(false);

Hide/Show field

Xrm.Page.ui.controls.get(fieldName).setVisible(false);
Xrm.Page.ui.controls.get(fieldName).setVisible(true);

Get CRM Form Type

var FORM_TYPE_CREATE = 1;
var FORM_TYPE_UPDATE = 2;
var FORM_TYPE_READ_ONLY = 3;
var FORM_TYPE_DISABLED = 4;
var FORM_TYPE_QUICK_CREATE = 5;
var FORM_TYPE_BULK_EDIT = 6;

var formType = Xrm.Page.ui.getFormType();
if (formType == FORM_TYPE_CREATE) {
}
else {
}

Cheers – Sy

Advertisements

5 thoughts on “CRM 2011 Useful JavaScript tidbits

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s