[TIP] Setting Default Values for Option Sets (Choice) in Dynamics 365 Using JavaScript

 

Setting Default Values for Option Sets (Choice) in Dynamics 365 Using JavaScript

Option Sets, also known as Choice fields in the latest version of Dataverse, are commonly used in Dynamics 365 to allow users to select from a pre-defined list of options. In some instances, you may want to set a default value for an Option Set (Choice) when a form is loaded. This can be achieved using JavaScript. Below are examples for both single-select and multi-select Option Sets (Choices).

Single-Select Option Set (Choice)

For a single-select Option Set (Choice), you use the getValue and setValue methods to read and set the value of the field.

Here's a JavaScript function to set a default value for a single-select Option Set (Choice) field named kc_mealhandling. The default value is 948040007.

javascript
// Function to set default value for single-select Option Set (Choice) function setDefaultMealHandlingOption(executionContext) { // Get form context var formContext = executionContext.getFormContext(); // Define field name and default value var fieldName = "kc_mealhandling"; var defaultValue = 948040007; // Get Option Set field from form var optionSetField = formContext.getAttribute(fieldName); // Check if the field is null or already has a value if (optionSetField != null && optionSetField.getValue() === null) { // Set the default value optionSetField.setValue(defaultValue); } }

Multi-Select Option Set (Choice)

For Multi-Select Option Sets (Choices), the code is similar but allows for multiple values to be selected. You still use getValue and setValue, but the value you set will be an array.

Here's the JavaScript function to set a default value for a multi-select Option Set (Choice):

javascript
// Function to set default value for multi-select Option Set (Choice) function setDefaultMealHandlingMultiSelect(executionContext) { // Get form context var formContext = executionContext.getFormContext(); // Define field name and default values array var fieldName = "kc_mealhandling"; var defaultValues = [948040007]; // Get Option Set field from form var optionSetField = formContext.getAttribute(fieldName); // Check if the field is null or already has a value if (optionSetField != null && optionSetField.getValue() === null) { // Set the default value optionSetField.setValue(defaultValues); } }

Summary of Differences

The key differences between setting default values for single-select and multi-select Option Sets (Choices) are:

  1. Default Value Type: In single-select, the default value is a single integer. For multi-select, the default value is an array of integers.

  2. Setting Value: In both cases, you use setValue, but for multi-select, you pass an array even if it's just one default value.

By using JavaScript, you can effectively set default values for both single-select and multi-select Option Sets (Choices) in Dynamics 365. These scripts can be added as Web Resources in Dynamics 365 and then attached to the form's OnLoad event for the respective entities.

No comments:

Post a Comment