Microsoft Dynamics CRM
Open New Dynamics CRM Record with Pre-Populated Fields
A very common JavaScript customization is to open another Entity with pre-populated Fields from the original Entity. This is achieved using the “openEntityForm” method provided by the Microsoft SDK. In the example below, this code could be triggered from the Opportunity to open a Phone Activity that populates the “Name,” “Regarding,” and “Regarding Type” from the Opportunity.
var lookupItems = {};
lookupItems["parameter_regardingname"] = Xrm.Page.getAttribute("name").getValue();
lookupItems["parameter_regardingid"] = Xrm.Page.data.entity.getId();
lookupItems["parameter_regardingtype"] = "opportunity";
Xrm.Utility.openEntityForm("phonecall", null, lookupItems);
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
———————————————-
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);
}
———————————————-