Useful Javascripts for CRM 2011


// Get the form type
GetFormType: function () {
 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) {
  alert("This record has not yet been created.");
 }
 else {
  alert("This record exists in the database.");
 }
}




// Registering an event for a custom control, and call a method when it fires
RegisterEventForACustomControl: function (var controlId) {
        var customField = document.getElementById(controlId);
        /* Build Toggle Function */
        var f = "var ef=function() { " +
                  "MethodToBeCalled();" +
                  " };";
        eval(f);
        /* Attach to click event */
        customField.attachEvent('onclick', ef, false);
}

MethodToBeCalled: function () {
        // Do something
}




// Disable/Enable all controls in a Tab
DisableAllControlsInTab: function (tabName, isDisable) {
    var tabControl = Xrm.Page.ui.tabs.get(tabName);
    if (tabControl != null) {
        Xrm.Page.ui.controls.forEach(
        function (control, index) {
            if (control.getParent().getParent() == tabControl && control.getControlType() != "subgrid") {
                control.setDisabled(isDisable);
            }
        });
    }
}




// Get the value from a CRM field
var varMyValue = Xrm.Page.getAttribute("CRMFieldSchemaName”).getValue() ;

// Set the value of a CRM field
Xrm.Page.getAttribute("CRMFieldSchemaName”).setValue(‘New Value’);




// Hide/Show a tab using 
Xrm.Page.ui.tabs.get("CRMTabName").setVisible(true);
Xrm.Page.ui.tabs.get("CRMTabName").setVisible(false);

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




// Hide/Show a section in a tab
Xrm.Page.ui.tabs.get("CrmTabName").sections.get("SectionName").setVisible(true);
Xrm.Page.ui.tabs.get("CrmTabName").sections.get("SectionName").setVisible(false);




// Call the onchange event of a field
Xrm.Page.getAttribute("CRMFieldSchemaName”).fireOnChange();




// Get the selected value of picklist
Xrm.Page.getAttribute("CRMFieldSchemaName”).getSelectedOption().getValue();




// 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);




// Return array of strings of users security role GUIDs:
Xrm.Page.context.getUserRoles()




// Setting Force Submit to the field, This must be set to save the data
Xrm.Page.getAttribute("CRMFieldSchemaName”).setSubmitMode("always”);




// Stop an on save event
event.returnValue = false;




// Form close
Xrm.Page.ui.close();


Advertisement

Author: Indika Abayarathne

MSc in IT [University of Colombo] Solutions Architect | Consultant Technologies: Power Platform | Dynamics CE | Azure

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: