Dynamics 365/CRM has come a long way since the 4.0 and 2011 days when JavaScript was the only way to meet most custom requirements. We now have business rules, roll-up fields, calculated fields, process flows and more that can be configured to meet custom requirements right within the GUI (which is the recommended path when you can get away with it). But for the times when you can’t get what you need from the front end, here’s a quick D365/CRM
Getting and setting field values:
//------------------------------------------------------------------------------------------- | |
// getTextField | |
//------------------------------------------------------------------------------------------- | |
function getTextField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Get the value of a Contact's first name | |
var firstName = formContext.getAttribute("firstname").getValue(); | |
} |
//------------------------------------------------------------------------------------------- | |
// setTextField | |
//------------------------------------------------------------------------------------------- | |
function setTextField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set the Contact's first name to Joe | |
formContext.getAttribute("firstname").setValue("Joe"); | |
// Set the Contact's first name to a variable | |
var firstName = "Joe"; | |
formContext.getAttribute("firstname").setValue(firstName); | |
} |
//------------------------------------------------------------------------------------------- | |
// getLookupFieldText | |
//------------------------------------------------------------------------------------------- | |
function getLookupFieldText(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Get the Parent Company name of a Contact | |
var companyName = formContext.getAttribute("parentcustomerid").getValue()[0].name; | |
} |
//------------------------------------------------------------------------------------------- | |
// setLookupField | |
//------------------------------------------------------------------------------------------- | |
function setLookupField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// First, you need the GUID, Name and Entity Type stored as variables | |
// For this example, let's assume we have a "Parent Customer 2" field | |
// on a Contact and we want to populate it with the same value as the | |
// Parent Customer | |
// Get the GUID | |
var id = formContext.getAttribute("primarycustomerid").getValue()[0].id; | |
// Get the Company Name | |
var name = formContext.getAttribute("primarycustomerid").getValue()[0].name; | |
// Set the entity type - Account, Contact, Lead, Opportunity, etc. | |
var entityType = "Account"; | |
// If the ID isn't null, then create a variable to store the new Object | |
if (id != null) { | |
var lookupVal = new Array(); | |
lookupVal[0] = new Object(); | |
lookupVal[0].id = guid; | |
lookupVal[0].name = name; | |
lookupVal[0].entityType = entityType; | |
// Set the value of the custom "Parent Customer 2" field | |
formContext.getAttribute("new_parentcustomerid2").setValue(lookupVal); | |
} | |
} |
//------------------------------------------------------------------------------------------- | |
// getNumericField | |
//------------------------------------------------------------------------------------------- | |
function getNumericField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Get the value of a custom Age field on a Contact | |
var age = formContext.getAttribute("new_age").getValue(); | |
} |
//------------------------------------------------------------------------------------------- | |
// setNumericField | |
//------------------------------------------------------------------------------------------- | |
function setNumericField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set the Contact's age to 25 | |
formContext.getAttribute("new_age").setValue(25); | |
// Set the Contact's age to a variable | |
var age = 25; | |
Xrm.Page.data.entity.attributes.get("new_age").setValue(age); | |
} |
//------------------------------------------------------------------------------------------- | |
// getBitField | |
//------------------------------------------------------------------------------------------- | |
function getBitField(e) { | |
// Get the value of a custom "Is Decision Maker" checkbox on a Contact | |
var decisionMaker = formContext.getAttribute("new_isDecisionMaker").getValue(); | |
// checked will return 1; unchecked will return 0; | |
} |
//------------------------------------------------------------------------------------------- | |
// setBitField | |
//------------------------------------------------------------------------------------------- | |
function setBitField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set the value of a custom "Is Decision Maker" checkbox to checked | |
formContext.getAttribute("new_isDecisionMaker").setValue(1); | |
// Set the value of a custom "Is Decision Maker" checkbox to unchecked | |
formContext.getAttribute("new_isDecisionMaker").setValue(0); | |
} |
//------------------------------------------------------------------------------------------- | |
// getOptionSetField | |
//------------------------------------------------------------------------------------------- | |
function getOptionSetField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Get the database value of the Opportunity Status Reason field | |
var statusReason = formContext.getAttribute("statuscode").getValue(); | |
// Get the text of the Opportunity Status Reason field | |
var statusReason = formContext.getAttribute("statuscode").getText(); | |
} |
//------------------------------------------------------------------------------------------- | |
// setOptionSetField | |
//------------------------------------------------------------------------------------------- | |
function setOptionSetField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set the value of the Opportunity Status Reason field | |
// Note: Option Set fields are set based on the database id, rather than the name | |
formContext.getAttribute("statuscode").setValue(100000001); | |
// Set the value of the Opportunity Status Reason field to a variable | |
var statusReasonId = 100000001; | |
formContext.getAttribute("statuscode").setValue(statusReasonId); | |
} |
//------------------------------------------------------------------------------------------- | |
// getDateField | |
//------------------------------------------------------------------------------------------- | |
function getDateField(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Get the value of the Opportunity Est. Close Date | |
var date = formContext.getAttribute("estimatedclosedate").getValue(); | |
if (date != null) { | |
var day = date.getDate(); | |
var month = date.getMonth(); | |
var year = date.getFullYear(); | |
month++; | |
// Add leading 0's to single digit days and months | |
if (day < 10) {day = "0" + day;} | |
if (month < 10) {month = "0" + month;} | |
// Create a variable with the formatted date as MM/DD/YYYY | |
var formattedDate = month + "/" + day + "/" + year; | |
// Create a variable with the formatted date as DD/MM/YYYY | |
//var formattedDate = date + "/" + month + "/" + year; | |
} | |
} |
Customizing forms:
//------------------------------------------------------------------------------------------- | |
// showHideFormSections | |
//------------------------------------------------------------------------------------------- | |
// Consider using Business Rules before using this code | |
function showHideFormSections(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Create variables for the tab an section | |
var tab=formContext.ui.tabs.get("TabName"); | |
var section = tab.sections.get("SectionName"); | |
// Hide a section | |
section.setVisible(false); | |
// Show a section | |
section.setVisible(true); | |
} |
//------------------------------------------------------------------------------------------- | |
// showHideFields | |
//------------------------------------------------------------------------------------------- | |
// Consider using Business Rules before using this code | |
function showHideFields(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Show a form field | |
formContext.getControl("new_formField").setVisible(true); | |
// Hide a form field | |
formContext.getControl("new_formField").setVisible(false); | |
} |
//------------------------------------------------------------------------------------------- | |
// setRequiredLevel | |
//------------------------------------------------------------------------------------------- | |
function setRequiredLevel(e){ | |
// Consider using Business Rules before using this code | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set Opportunity Est. Close Date as optional | |
formContext.getAttribute("estimatedclosedate").setRequiredLevel("none"); | |
// Set Opportunity Est. Close Date as required | |
formContext.getAttribute("estimatedclosedate").setRequiredLevel("required"); | |
} |
//------------------------------------------------------------------------------------------- | |
// setCustomFormLayout | |
//------------------------------------------------------------------------------------------- | |
function setCustomFormLayout(e){ | |
// Consider using Business Rules before using this code | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// This example assumes a company uses the same Opportunity form, | |
// but shows/hides sections and makes fields required/optional based | |
// on the value of a new_division field | |
// hook this up to the onLoad event and the onChange event of the new_division field | |
// Create variables for the form sections | |
var tab = formContext.ui.tabs.get("tab_division"); | |
var sectionComputers = tab.sections.get("section_computers"); | |
var sectionMonitors = tab.sections.get("section_monitors"); | |
var sectionPeripherals = tab.sections.get("section_peripherals"); | |
// Set all sections as hidden | |
sectionComputers.setVisible(false); | |
sectionMonitors.setVisible(false); | |
sectionPeripherals.setVisible(false); | |
// Set all required fields as not required | |
formContext.getAttribute("new_computerField").setRequiredLevel("none"); | |
formContext.getAttribute("new_monitorField").setRequiredLevel("none"); | |
formContext.getAttribute("new_peripheralField").setRequiredLevel("none"); | |
// Get the text of the Division field | |
var division = formContext.getAttribute("new_division").getText(); | |
// Make sure Division is populated | |
if (division != null) { | |
// Use a switch statement to set up the form for each division | |
switch(division) { | |
// Computer division | |
case "Computers": | |
sectionComputers.setVisible(true); | |
formContext.getAttribute("new_computerField").setRequiredLevel("required"); | |
break; | |
// Monitor division | |
case "Monitors": | |
sectionMonitors.setVisible(true); | |
formContext.getAttribute("new_monitorField").setRequiredLevel("required"); | |
break; | |
// Peripherals division | |
case "Peripherals": | |
sectionPeripherals.setVisible(true); | |
formContext.getAttribute("new_peripheralField").setRequiredLevel("required"); | |
break; | |
} | |
} | |
} |
//------------------------------------------------------------------------------------------- | |
// openSpecificForm | |
//------------------------------------------------------------------------------------------- | |
function openSpecificForm(e){ | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
//if the form is update form | |
if (formContext.ui.getFormType()==2) { | |
// variable to store the name of the form | |
var lblForm; | |
// get the value picklist field | |
var relType = formContext.getAttribute("customertypecode").getValue(); | |
// switch statement to assign the form to the picklist value | |
// change the switch statement based on the forms numbers and picklist values | |
switch (relType) { | |
case 1: | |
lblForm = "Information1"; | |
break; | |
case 2: | |
lblForm = "Information2"; | |
break; | |
default: | |
lblForm = "Information"; | |
} | |
//check if the current form is form need to be displayed based on the value | |
if (formContext.ui.formSelector.getCurrentItem().getLabel() != lblForm) { | |
var items = Xrm.Page.ui.formSelector.items.get(); | |
for (var i in items) { | |
var item = items[i]; | |
var itemId = item.getId(); | |
var itemLabel = item.getLabel() | |
if (itemLabel == lblForm) { | |
//navigate to the form | |
item.navigate(); | |
} | |
} | |
} | |
} | |
} |
Pulling data from related entities
Pulling data from related entities
//------------------------------------------------------------------------------------------- | |
// getRelatedInfo | |
//------------------------------------------------------------------------------------------- | |
function getRelatedInfo(e) { | |
// Get the Form Context | |
var formContext = e.getFormContext(); | |
// Set variables for the related entity to get data from | |
try{var recordGuid = formContext.getAttribute("lookupField").getValue()[0].id; } | |
catch(err) {} | |
// If the recordGuid contains data | |
if (recordGuid != null) { | |
// Use the WebApi to get data | |
Xrm.WebApi.retrieveRecord("entityName", recordGuid, "?$select=name,revenue").then( | |
// If successful, set variables for the results | |
function success(result) { | |
var abc = result.field1Name; | |
var def = result.field2Name; | |
// Set the form fields | |
formContext.getAttribute("field1").setValue(abc); | |
}, | |
// If there's an error in the WebApi call, log the error and alert the user | |
function (error) { | |
console.log(error.message); | |
// handle error conditions | |
var errorMessage = 'Error: An error occurred in the GetRelatedInfo function: ' + error.message; | |
var alertStrings = { confirmButtonLabel: "Yes", text: errorMessage }; | |
var alertOptions = { height: 120, width: 260 }; | |
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( | |
function success(result) { | |
console.log("Alert dialog closed"); | |
}, | |
function (error) { | |
console.log(error.message); | |
} | |
); | |
} | |
); | |
} | |
} |
No comments:
Post a Comment