Monday, September 12, 2011

CRM 2011 Useful JavaScript

Get the value from a CRM field

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

Set the value of a CRM field

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

Hide/Show a tab

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

Hide/Show a section

 
1Xrm.Page.ui.tabs.get(5).sections.get(0).setVisible(false);
2Xrm.Page.ui.tabs.get(5).sections.get(0).setVisible(true);

Call the onchange event of a field

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

Get the selected value of picklist

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

Set the requirement level

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

Set the focus to a field

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

Stop an on save event

 
1event.returnValue = false;

Return array of strings of users security role GUIDs:

 
1Xrm.Page.context.getUserRoles()

Get record GUID

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

Get event source

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

Get event source (fieldname)

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

Get event source (value)

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

Set Lookup Value

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

Disable/Enable field

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

field

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

Get CRM Form Type

 
01var FORM_TYPE_CREATE = 1;
02var FORM_TYPE_UPDATE = 2;
03var FORM_TYPE_READ_ONLY = 3;
04var FORM_TYPE_DISABLED = 4;
05var FORM_TYPE_QUICK_CREATE = 5;
06var FORM_TYPE_BULK_EDIT = 6;
07  
08var formType = Xrm.Page.ui.getFormType();
09if (formType == FORM_TYPE_CREATE) {
10}
11else {
12}

No comments:

Post a Comment