Auxiliary Force Technology

Dynamics CRM JavaScript Code Basics

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.

———————————————-
Get Attribute Value
———————————————-

Version 9.0+ and Power Apps

function getAttribute(executionContext) {

var formContext = executionContext.getFormContext();
var attributeValue = formContext.getAttribute("attributename").getValue();
}

Version 8.2-

var variable = Xrm.Page.getAttribute("attributename").getValue();
———————————————-
Set Attribute Value
———————————————-

Version 9.0+ and Power Apps

function getAttribute(executionContext) {

var formContext = executionContext.getFormContext();
var attributeValue = formContext.getAttribute("attributename").setValue(value)
}

Version 8.2-

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
———————————————-

Version 9.0+ and Power Apps

function getAttribute(executionContext) {

var formContext = executionContext.getFormContext();
formContext.ui.tabs.get("tabname").setVisible(true);
}

Version 8.2-

Xrm.Page.ui.tabs.get("tabname").setVisible(true);
Xrm.Page.ui.tabs.get("tabname").setVisible(false);

———————————————-
Get Lookup Value
———————————————-

Version 9.0+ and Power Apps

function getAttribute(executionContext) {

var formContext = executionContext.getFormContext();

var attributeValue = formContext.getAttribute("attributename").getValue();
}

if (attributeValue != null) {
var name = attributeValue[0].name;
var id = attributeValue[0].id;
var entityType = attributeValue[0].entityType;
}

Version 8.2-

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
———————————————-

Version 9.0+ and Power Apps

function setLookupValue(e) {
var formContext = e.getFormContext();

var lookup = {
"entityType": "contact",
"id": "00000-0000-0000-0000-000000000000",
"name": "name of record"
};

formContext.getAttribute("fieldid").setValue([lookup]);
}

Version 8.2-

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);
}

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