Labels

Tuesday, December 9, 2014

CRM 2011 Gregorian to Hijri conversion

CRM 2011 Gregorian to Hijri conversion Javascript

function gmod(n, m) {
    return ((n % m) + m) % m;
}

function kuwaiticalendar(adjust) {
    //var today = new Date();
    var today = Xrm.Page.getAttribute("inf_g2hdate").getValue();
    if (adjust) {
        adjustmili = 1000 * 60 * 60 * 24 * adjust;
        todaymili = today.getTime() + adjustmili;
        today = new Date(todaymili);
    }
    day = today.getDate();
    month = today.getMonth();
    year = today.getFullYear();
    m = month + 1;
    y = year;
    if (m < 3) {
        y -= 1;
        m += 12;
    }

    a = Math.floor(y / 100.);
    b = 2 - a + Math.floor(a / 4.);
    if (y < 1583) b = 0;
    if (y == 1582) {
        if (m > 10) b = -10;
        if (m == 10) {
            b = 0;
            if (day > 4) b = -10;
        }
    }

    jd = Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + day + b - 1524;

    b = 0;
    if (jd > 2299160) {
        a = Math.floor((jd - 1867216.25) / 36524.25);
        b = 1 + a - Math.floor(a / 4.);
    }
    bb = jd + b + 1524;
    cc = Math.floor((bb - 122.1) / 365.25);
    dd = Math.floor(365.25 * cc);
    ee = Math.floor((bb - dd) / 30.6001);
    day = (bb - dd) - Math.floor(30.6001 * ee);
    month = ee - 1;
    if (ee > 13) {
        cc += 1;
        month = ee - 13;
    }
    year = cc - 4716;


    wd = gmod(jd + 1, 7) + 1;

    iyear = 10631. / 30.;
    epochastro = 1948084;
    epochcivil = 1948085;

    shift1 = 8.01 / 60.;

    z = jd - epochastro;
    cyc = Math.floor(z / 10631.);
    z = z - 10631 * cyc;
    j = Math.floor((z - shift1) / iyear);
    iy = 30 * cyc + j;
    z = z - Math.floor(j * iyear + shift1);
    im = Math.floor((z + 28.5001) / 29.5);
    if (im == 13) im = 12;
    id = z - Math.floor(29.5001 * im - 29);

    var myRes = new Array(8);

    myRes[0] = day; //calculated day (CE)
    myRes[1] = month - 1; //calculated month (CE)
    myRes[2] = year; //calculated year (CE)
    myRes[3] = jd - 1; //julian day number
    myRes[4] = wd - 1; //weekday number
    myRes[5] = id; //islamic date
    myRes[6] = im - 1; //islamic month
    myRes[7] = iy; //islamic year

    return myRes;
}

function writeIslamicDate(adjustment) {
    var iMonthNames = new Array(1,2,3,4,5,6,7,8,9,10,11,12);
    var iDate = kuwaiticalendar(adjustment);
    var outputIslamicDate = iDate[5] + "/" + iMonthNames[iDate[6]] + "/" + iDate[7];
    //alert(outputIslamicDate);
    Xrm.Page.getAttribute("Field Name").setValue(outputIslamicDate);
    return outputIslamicDate;
}

Monday, December 1, 2014

Help Full  Javascript  for CRM 2011


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

Set the value of a CRM field

Xrm.Page.getAttribute("CRMFieldSchemaName ").setValue("New Value");

Get the value from a CRM OptionSet field

var value = Xrm.Page.getAttribute("CRMOptionSetSchemaName").getValue();

Get the text from a CRM OptionSet field

var text = Xrm.Page.getAttribute("CRMOptionSetSchemaName").getText();

Set the value of a CRM OptionSet field

Xrm.Page.getAttribute("CRMOptionSetSchemaName").setValue("1"); // OptionSet Value

Get the selected text of a CRM OptionSet field

Xrm.Page.getAttribute("CRMOptionSetSchemaName").getSelectedOption().text;

Get the selected value of a CRM OptionSet field

Xrm.Page.getAttribute("CRMOptionSetSchemaName").getSelectedOption().value;

Get the text and value of a CRM Lookup field

var lookupObject = Xrm.Page.getAttribute("CRMLookupSchemaName").getValue();
lookupObject[0].name; // text of lookup
lookupObject[0].id; // Guid of lookup

Set the value of a CRM Lookup field

var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = “4A2A54CB-349C-E111-8D26-1CC1DEE8DA78?; // Guid of record
lookupItem.name = “New Contact”; // Entity record name
lookupItem.entityType = “EntitySchemaName”;
lookupData[0] = lookupItem;
Xrm.Page.getAttribute("CRMLookupSchemaName").setValue(lookupData);

Disable CRM field

Xrm.Page.ui.controls.get("CRMFieldSchemaName").setDisabled(true);

Hide CRM field

Xrm.Page.ui.controls.get("CRMFieldSchemaName").setVisible(false);

Hide a Tab in CRM

Xrm.Page.ui.tabs.get(“tabName”).setVisible(false);

Hide a Section in CRM

var tab = Xrm.Page.ui.tabs.get(“tabName”);
tab.sections.get(“sectionName”).setVisible(false);

Set the Requirement level in CRM

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

Set Focus on a field in CRM

Xrm.Page.ui.controls.get(“CRMFieldSchemaName”).setFocus(true);

Cancelling Onsave Event in CRM

event.returnValue = false;
return false;

Check IsDirty in CRM field

var isDirty = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getIsDirty();
alert(isDirty); // returns true if the field is dirty

Check IsDirty for all the fields in CRM

var isDirty = Xrm.Page.data.entity.getIsDirty();
alert(isDirty); // returns true if any of the field is dirty in the entire form.

Force Submit a read only field in CRM

Xrm.Page.getAttribute(“CRMFieldSchemaName”).setSubmitMode(“always”);

Preventing an attribute to be saved in CRM form

Xrm.Page.getAttribute(“CRMFieldSchemaName”).setSubmitMode(“never”);

Get Unique Organization Name in CRM

Xrm.Page.context.getOrgUniqueName();

Get Server url in CRM

Xrm.Page.context.getServerUrl();

Xrm.Page.data.entity.getId();

Get the User Id in CRM

Xrm.Page.context.getUserId();

Get the Entity Schema Name in CRM

Xrm.Page.data.entity.getEntityName();

Get the UserRole Id’s in CRM

var userRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < userRoles.length; i++)
{
var userRole = userRoles[i]; // returns the Role Id
}

Get the Form Type in CRM

Xrm.Page.ui.getFormType();

Form Types in CRM

Is the user creating a new record?
Xrm.Page.ui.getFormType() == "1"

Is the user updating an existing record?

Xrm.Page.ui.getFormType() == "2"

Is the user unable to update this record?

Xrm.Page.ui.getFormType() == “3"

Is this record deactivated?

Xrm.Page.ui.getFormType() == “4"

Is the user using the Quick Create form?

Xrm.Page.ui.getFormType() == “5?

Is the user using the Bulk Edit form?

Xrm.Page.ui.getFormType() == “6"

Save a record in CRM 

Xrm.Page.data.entity.save(); // for saving a record
Xrm.Page.data.entity.save(“saveandclose”); // for save and close
Xrm.Page.data.entity.save(“saveandnew”); // for save and new

Close the form in CRM

Xrm.Page.ui.close();

get Id of the record


var Id = Xrm.Page.data.entity.getId();


Form Label

var formLabel = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();