Image of field required level in Dynamics 35

JavaScript Code Snippets for Dynamics 365 – Cheat Sheet

If you think your friends/network would find this useful, please share it with them – I’d really appreciate it.

When writing custom JavaScript code for Dynamics there are often many of the same code lines used when adding functionality to forms, fields or ribbon buttons. In this post I’ve collected some short code snippets that I often use. The examples is meant to be a look up guide or a cheat sheet to have a place to quickly get the snippet needed.

In the screenshots in this post there is used Xrm.Page method instead of the formContext on some examples. There reason behind this is that you doesn’t need to add a function to a form to see what the code does. The Xrm.Page is deprecated but I find it useful to find values and to see what the code does before I add it to a form.

Below you will find different snippets and screenshots of the result in the developer console. This is no complete cheat sheet list, but some of the snippets that came to mind in the time of writing.

How to get the current row id and replace the brackets

let contactId = formContext.data.entity.getId().replace(/{|}/g, '');
Output of getting current row id in Dynamics 365/Dataverse.

How to get the entity reference of the current row

Return the look up value of the current record. Another way to retrieve current record id.

formContext.data.entity.getEntityReference();
Output of getEntityReference() function in Dynamics 365/Dataverse.

How to get parent account related to a contact

let contactId = formContext.data.entity.getId().replace(/{|}/g, '');
let result = await Xrm.WebApi.retrieveRecord("contact", contactId, "?$select=firstname,lastname,contactid&$expand=parentcustomerid_account($select=accountid,name)");
let parentAccount = result['parentcustomerid_account'];
let accountId = result['parentcustomerid_account'].accountid;
let accountName = result['parentcustomerid_account'].name;

console.log("contactId: ", contactId);
console.log("result: ", result);
console.log("parentAccount: ",parentAccount);
console.log("accountId : ",accountId);
console.log("accountName : ", accountName);
Output when retrieving parent account related to a contact in Dynamics 365/Dataverse.

How to get primary contact for an account

let accountId = formContext.data.entity.getId().replace(/{|}/g, '');
let result = await Xrm.WebApi.retrieveRecord("account", accountId, "?$select=name&$expand=primarycontactid($select=contactid,fullname)")
let primaryContact = result['primarycontactid'];
let contactId = result['primarycontactid'].contactid;
let contactName = result['primarycontactid'].fullname;

console.log("primaryContact: ",primaryContact);
console.log("contactId: ",contactId);
console.log("contactName: ", contactName);
Output when retrieving primary contact on an account in Dynamics 365/Dataverse.

How to get values from look up field

let primaryContact = formContext.getAttribute("primarycontactid").getValue();
let primaryContactId = primaryContact[0].id.replace(/{|}/g, "");
let primaryContacName = primaryContact[0].name;

console.log("primaryContact: ", primaryContact);
console.log("primaryContactId: ", primaryContactId);
console.log("primaryContacName: ", primaryContacName);
Output of values from a look up field in Dynamics 365/Dataverse.

How to get form type

let formType = formContext.ui.getFormType();

// 0 Undefined
// 1 Create
// 2 Update
// 3 Read Only
// 4 Disabled
// 6 Bulk Edit
// Quick Create forms return 1
Image of the form type in Dynamics 365/Dataverse.

How to show and hide a field on a form

//Show
formContext.getControl("FieldName").setVisible(false);

//Hide
formContext.getControl("FieldName").setVisible(false);

How to show and hide section on a form

// Hide section within a specified tab
let tab = formContext.ui.tabs.get("TabName");
let section = tab.sections.get("SectionName");
section.setVisible(false);

// Show section within a specified tab
let tab = formContext.ui.tabs.get("TabName");
let section = tab.sections.get("SectionName");
section.setVisible(true);

How to set required fields in Dynamics 365 with JavaScript

Sets a field to required, recommended, or none.

formContext.getAttribute("fieldname").setRequiredLevel("required");
formContext.getAttribute("fieldname").setRequiredLevel("recommended");
formContext.getAttribute("fieldname").setRequiredLevel("none");
Image of examples of required, recommended and none required levels in Dynamics 365/Dataverse.

How to set tab in focus

Sets the desired tab in focus.

let defaultTab = formContext.ui.navigation("tab").getId();
formContext.ui.navigation.setFocus();

How to set value in a look up field

let lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = "465b158c-541c-e511-80d3-3863bb347ba8";
lookupValue[0].entityType = "contact";
			
formContext.getAttribute("primarycontactid").setValue(lookupValue);

How to get current app url

let globalContext = Xrm.Utility.getGlobalContext();
globalContext.getCurrentAppUrl();

Retrieves the following:

Image of the output of get current app url in Dynamics 365/Dataverse.

How to get current app id and app display name

let globalContext = Xrm.Utility.getGlobalContext();
let appProperties = await globalContext.getCurrentAppProperties();

let appId = appProperties.appId;
let displayName = appProperties.displayName;
let uniqueName = appProperties.uniqueName;

// Output
console.log("appId: ", appId);
console.log("displayName: ", displayName);
console.log("uniqueName: ", uniqueName);

Retrieves the following:

Image of current app id and display name in Dynamics 365/Dataverse.

How to get current user id

let userSettings = Xrm.Utility.getGlobalContext().userSettings;
let userId = userSettings.userId;

Retrieves the following:

Image of the output from getting current user id in Dynamics 365/Dataverse.

How to get security roles of current user

let userSettings = Xrm.Utility.getGlobalContext().userSettings
let securityRoles = userSettings.securityRoles;

Retrieves the following:

Image of the output from getting security roles from current user in Dynamics 365/Dataverse.

How to get user settings of current user

let userSettings = Xrm.Utility.getGlobalContext().userSettings;

Retrieves the following: