Sunday, May 13, 2012

Dynamics CRM 2011 Useful Javascripts

During our CRM 2011 javascript development, we need few javascript snippets over and over. So i am planning to put those common scripts in to one common file. So, we can refer the same file by calling just thefunctions with parameters.

1. Hide/ Show  form fields.

//VobjField   -  Name of field to show/hide.
//VblnVisible -  True to show the field, False to hide.
function ShowHideField(VobjField, VblnVisible){
            objField = Xrm.Page.ui.controls.get(VobjField);
            objField.setVisible(VblnVisible);
}

2. Set the CRM 2011 form field Submit Mode

// SobjField      - Name of the field to move to
// SstrSubmitMode - must be one of "always", "never" or "dirty"
function SetFieldSubmitMode(SobjField, SstrSubmitMode) {
        objField = Xrm.Page.ui.controls.get(SobjField);
        objField.setSubmitMode(SstrSubmitMode);
}

3.  Relabel the field

//  SobjField  - Name of field to relabel.
//  SstrNewFieldLabel -  New Label for the label.
function RelabelField(SobjField, SstrNewFieldLabel){
            objField = Xrm.Page.ui.controls.get(SobjField);
            objField.setLabel(SstrNewFieldLabel);
}

4. Enable /Disble the form field

// SobjFieldName - name of the field.
// SblnDisabled  - True to make field disabled, False to not.
function SetDisabledLevel(SobjFieldName, SblnDisabled) {
            objField = Xrm.Page.ui.controls.get(SobjFieldName);
            objField.setDisabled(SblnDisabled);
}

5. Show/ Hide the sction on the specific tab

// SstrTabName     - Name of the tab to locate.
// SstrSectionName - Name of the Section to locate.
// SblnVisible     - True to show the tab section, False to hide.
function ShowHideSection(SstrTabName, SstrSectionName, SblnVisible){
        objSectionItem =  Xrm.Page.ui.tabs.get(SstrTabName).sections.get(SstrSectionName);
        objSectionItem.setVisible(SblnVisible);
}

6. Show  / Hide  Tab on the form

//  SstrTabName  - Name of the tab to locate.
//  SblnVisible  - True to show the tab, False to hide.
function ShowHideTab(SstrTabName, SblnVisible){
        objTabItem = Xrm.Page.ui.tabs.get(SstrTabName);        
        objTabItem.setVisible(SblnVisible);
}

7. Set the Requirment level of the form field

// SobjFieldName  - name of the field.
// SstrRequirementLevel - none, required or recommended
function SetRequirementLevel(SobjFieldName, SstrRequirementLevel) {
                objField = Xrm.Page.data.entity.attributes.get(SobjFieldName);
                objField.setRequiredLevel(SstrRequirementLevel);
}

8. Collapse the Tab

// tabName  -Tab name to be collapsed
function CollapseTab(tabName) {
        var control = Xrm.Page.ui.tabs.get(tabName);
        control.setDisplayState("collapsed");
}

9. Expand the Tab

// tabName -  Tab name to be expanded
function ExpandTab(tabName) {
        var control = Xrm.Page.ui.tabs.get(tabName);
        control.setDisplayState("expanded");
}

10. Get the user Details

//Note : you need to inclde json.js file in order to run the script below.

function GetUserDetails(Scolumns) {
    var context = Xrm.Page.context;
    userID = context.getUserId();
    serverUrl = context.getServerUrl();
    ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    startTime = new Date();
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", ODataPath + "/SystemUserSet(guid'" + userID + "')?$select=" + Scolumns, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function() { RetrieveUserRequestCallBack(this); };
    retrieveReq.send();
}
function RetrieveUserRequestCallBack(responseReq) {
    if (responseReq.readyState == 4 /* complete */) {
        if (responseReq.status == 200) {
            try {
                //Success
                var retrievedUser = JSON.parse(responseReq.responseText).d;
                return retrievedUser;
            }
            catch (e) {
                alert("RetrieveUserRequestCallBack:", e.Message);
            }
        }
        else {
            //Failure
            alert("RetrieveUserRequestCallBack function failure END");
            return null;
        }
    }
}

Hope this will help to someone.

Cheers !.

No comments:

Post a Comment