Tuesday, 1 May 2012

CRM Javascript



Get and Set Values

 Xrm.Page.getAttribute('<schema name of the attribute>').setValue(null);
 var strAttributeValue = Xrm.Page.getAttribute('<schema name of the attribute>').getValue();

Get and Set Disabled Property

 Xrm.Page.ui.controls.get('<schema name of the attribute>').setDisabled(true); 

Get and Set Attribute Disabled Property

-- Set
// below line is used to disable the field.

Xrm.Page.ui.controls.get("<schema name of the attribute>").setDisabled(true);

 // below line is used to enable the disabled field.
 Xrm.Page.ui.controls.get("<schema name of the attribute>").setDisabled(false);

-- Get

var blnStatus = Xrm.Page.ui.controls.get('<schema name of the attribute>').getDisabled()


Setting RequiredLevel

There are required levels
1)Required
2)Recommended

3)None


Xrm.Page.getAttribute("<schema name of the attribute>").setRequiredLevel("required");

Xrm.Page.getAttribute("<schema name of the attribute>").setRequiredLevel("recommended");


Xrm.Page.getAttribute("<schema name of the attribute>").setRequiredLevel("none");


 To set the Focus
 Xrm.Page.getControl("<("<schema name of the attribute>").setFocus(true);

CRM FormTypes

crmForm.FormType = 1 // When creating a record

crmForm.FormType = 2 // When updating a record

crmForm.FormType = 3 // When the form is read-only


crmForm.FormType = 4 // De-activated or Disabled


crmForm.FormType = 5 // Quick Create

crmForm.FormType =  6// Bulk Edit


crmForm.ObjectId // To get the uniqueId associated with the record


crmForm.IsDirty== true // Have any fields on the form are changed


Method to Construct Date in JavaScript
function ConstructDate(Day, Month, Year) {

    var DateConstruct = new Date();

    DateConstruct.setDate(Day)

    // JavaScript Month starts from Index 0, so subtracted 1 form the original value.

    //    The parseInt function decides what base the number is by looking

    // at the number. By convention it assumes that any number beginning

    // with 0x is Hexadecimal, and otherwise any number beginning with

    // 0 is Octal. To force use of base 10 add a second parameter

    // `` parseInt("09",10) ''

    DateConstruct.setMonth(parseInt(Month, 10) - 1)

    DateConstruct.setYear(Year)

    DateConstruct.setHours(0, 0, 0, 0);

    return DateConstruct;

}


Function to format a date to the UTC format.

function DateToUTCFormat(inputDate) {
    var date = inputDate.getDate();
    var month = inputDate.getMonth() + 1;

    var year = inputDate.getYear();

    var hours = inputDate.getHours();

    var minutes = inputDate.getMinutes();

    var ampm = " AM";

    if (hours > 11) {

        ampm = " PM";

        hours = hours - 12;

    }

    if (hours == 0)

    { hours = 12; }

    if (minutes < 10) {

        var time = hours.toString() + ":0" + minutes.toString() + ":00" + ampm;

    }

    else {

        var time = hours.toString() + ":" + minutes.toString() + ":00" + ampm;

    }

    var UTCDate = month.toString() + "/" + date.toString() + "/" + year.toString() + " " + time;

    return UTCDate;

}


Get the Language code of the current user
 var UserLcid = Xrm.Page.context.getUserLcid();

Binding Click events
 crmForm.all.schemaname.onclick = function () {
            crmForm.all.schemaname.FireOnChange();

        }





Now,  it's time to write some generic methods in a single JavaScript and use them wherever we want...

//1. Hide/ Show  form fields.

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

//2. Set the CRM 2011 form field Submit Mode

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

//3.  Re-label the field

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


//4. Enable /Disable the form field

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

//5. Show/ Hide the section on the specific tab

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

//6. Show  / Hide  Tab on the form

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

//7. Set the Requirement level of the form field

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

//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 include 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;
        }
    }
}



No comments:

Post a Comment