Dynamics CRM JavaScript Code Basics (xRM 101)

Dynamics CRM JavaScript programming is actually far easier than most people would believe with so much code being re-used so frequently. While an exact percent would be impossible to achieve, it could be said that probably 80% of the code anyone will need for JavaScript Form customization can be can found in the following list.

With the advent of Business Rules in Dynamics 2013 many have come to erroneously conclude that JavaScript customization falls into either a “too complicated for the average Administrator” or “too basic that a Business Rule should be used instead.” Unfortunately, along with the advent of Business Rules came the advent of Business Rule frustrations such as:

Seemingly random order-of-operations.

Failing Business Rules that “should work.” and,

Business Rules that result in side-effect problems (try hiding Tabs with a non-Field element like a Quick View or Web Resource).

As such, having this arsenal of JavaScript commands will more successfully (and more efficiently enable the customization you need), enjoy!
———————————————-
Get Attribute Value
———————————————-
var variable = Xrm.Page.getAttribute("attributename").getValue();
———————————————-
Set Attribute Value
———————————————-
Xrm.Page.getAttribute("attributename").setValue(value);
———————————————-
Set Visibility
———————————————-
Xrm.Page.getControl("attributename").setVisible(false);
Xrm.Page.getControl("attributename").setVisible(true);

———————————————-
Make Attribute Required
———————————————-
Xrm.Page.getAttribute("attributename").setRequiredLevel("required");
Xrm.Page.getAttribute("attributename").setRequiredLevel("recommended");
Xrm.Page.getAttribute("attributename").setRequiredLevel("none");

———————————————-
Show/Hide Section
———————————————-
Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(true);
Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(false);

———————————————-
Show/Hide Tab
———————————————-
Xrm.Page.ui.tabs.get("tabname").setVisible(true);
Xrm.Page.ui.tabs.get("tabname").setVisible(false);

———————————————-
Get Lookup Value
———————————————-
var lookup = new Array();
lookup = Xrm.Page.getAttribute("attributename").getValue();
if (lookup != null) {
var name = lookup[0].name;
var id = lookup[0].id;
var entityType = lookup[0].entityType;
}

———————————————-
Set Lookup Value
———————————————-
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = recorid;
lookup[0].name = recordname;
lookup[0].entityType = entityname;
lookup[0].type = "lookup";
Xrm.Page.getAttribute("attributename").setValue(lookup);

OR

Xrm.Page.getAttribute("attributename").setValue([{ id: recorid, name: recordname, entityType: entityname, type: "lookup"}]);
———————————————-
Add Days, Years, etc. and Find Date/Day Information
———————————————-
function addSevenDays()
{
var originalDate = Xrm.Page.getAttribute("attributename").getValue();
newDate = new Date(originalDate.getFullYear(), originalDate.getMonth(), originalDate.getDate() + 7);

Xrm.Page.getAttribute("attributename").setValue(newDate);
}

———————————————-

Leave a Reply

Your email address will not be published. Required fields are marked *