Skip to content

Instantly share code, notes, and snippets.

@DynCon365
Last active April 26, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DynCon365/1a2b932cea846db94325f457d71208a5 to your computer and use it in GitHub Desktop.
Save DynCon365/1a2b932cea846db94325f457d71208a5 to your computer and use it in GitHub Desktop.
Dynamics 365/CRM | Use the value of an Option Set to set up a custom form layout
// Use the value of an Option Set to set up a custom form layout
// 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
// Consider using Business Rules before using this code
function opportunity_onLoad () {
// hook this up to the onLoad event and the onChange event of the new_division field
// Create variables for the form sections
var tab=Xrm.Page.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
Xrm.Page.getAttribute("new_computerField").setRequiredLevel("none");
Xrm.Page.getAttribute("new_monitorField").setRequiredLevel("none");
Xrm.Page.getAttribute("new_peripheralField").setRequiredLevel("none");
// Get the text of the Division field
var division = Xrm.Page.data.entity.attributes.get("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);
Xrm.Page.getAttribute("new_computerField").setRequiredLevel("required");
break;
// Monitor division
case "Monitors":
sectionMonitors.setVisible(true);
Xrm.Page.getAttribute("new_monitorField").setRequiredLevel("required");
break;
// Peripherals division
case "Peripherals":
sectionPeripherals.setVisible(true);
Xrm.Page.getAttribute("new_peripheralField").setRequiredLevel("required");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment