Power Apps: Use FetchXML Instead of OData In JavaScript

OData is awesome but there are some scenarios in which using FetchXML just makes more sense. For example, when searching NN Relationships. The following code uses FetchXML by encasing the query in the “backtick” character.

This code searches a custom “account_contactNN” many-to-many intersect using an Account ID to find all related Contacts. You may need to replace the “/api/data/v8.2/” portion with your version from “Developer Resources” in your Settings -> Customizations area.

Continue reading

Dynamics CRM Clone A Record With JavaScript

A very common request we often get is to have a clone record button added to an Entity. The JavaScript code tends to be the bigger issue than adding the ribbon button so to keep this post clean and direct the following code with cycle through every Field (unless one is excluded) to create a new record, ready for review, that can be saved as a clone. Continue reading

Setup Dynamics CRM E-mail Router with Office 365

Setting up the Dynamics CRM E-mail Router can be a bit tricky considering access rights to the Office 365 accounts. The long story short is that one account must be designated as the account through which all E-mail will pass. This means that account must have something called “Send As” privileges to the other Office 365 E-mail accounts. You can also use an access type called “Send On Behalf Of” but the difference lies in what the recipient sees:

If “Send As” is the method then the recipient just sees a normal E-mail address: user@company.com

If the “Send On Behalf Of” is the method then the recipient sees the routing account with a message that it is sent on behalf of: emailrouter@company.com “Sent on Behalf of: user@company.com”

As such, most people prefer to use the “Send As” option. This is how you setup a routing account with “Send As” privileges.

Continue reading

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

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