One of the noticeable changes between CRM 4.0 and CRM 2011 is the JavaScript object model. It has changed a bit. Below are some of the commonly used functions used to manipulate CRM forms. Let us know if you have a commonly used function that should be included in the list of CRM 2011 JavaScript tidbits below.

development

One of the tools we highly recommend is the JavaScript CRM 4 to CRM 2011 converter tool:

http://crm2011scriptconvert.codeplex.com/
Here are common 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/section:

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

Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()

Hide/Show Tabs and Sections:

function setVisibleTabSection(tabname, sectionname, show) {
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab != null) {
        if (sectionname == null)
            tab.setVisible(show);
        else {
            var section = tab.sections.get(sectionname);
            if (section != null) {
                section.setVisible(show);
                if (show)
                    tab.setVisible(show);
            }
        }
    }
}

Avatar for Joe D365

Joe D365

Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.