Filtering Option Set Values Based on Another Field in Dynamics 365

Filtering Option Set Values Based on Another Field in Dynamics 365 (Implementing Dynamic Option Set Filtering in Dynamics 365 Forms)

Introduction

This article provides a guide on customizing Dynamics 365 forms using JavaScript to dynamically filter option set values based on another field's value.

Scenario

Consider a situation where you have a form with two related fields: 'Type of Action' and 'Reason for Action'. The requirement is to filter the options available in the 'Reason for Action' field based on the selected value in the 'Type of Action' field. For example:

  • If 'Type of Action' is 'Positive Action', the 'Reason for Action' should show options like 'Exceptional Performance' and 'Extra Effort'.
  • If 'Type of Action' is 'Negative Action', the 'Reason for Action' should display options such as 'Policy Violation' and 'Performance Issue'.

Solution

The solution involves creating a JavaScript function that:

  1. Retrieves the value of the 'Type of Action' field.
  2. Clears existing options in the 'Reason for Action' field.
  3. Adds relevant options to the 'Reason for Action' field based on the 'Type of Action' value.

Sample Code

Below is an example JavaScript function for this purpose:

function filterReasonForActionOptions(executionContext)
{
    var formContext = executionContext.getFormContext();
    var actionType = formContext.getAttribute("type_of_action").getValue();
    var reasonForActionControl = formContext.getControl("reason_for_action");

    reasonForActionControl.clearOptions();

    if(actionType === "Positive_Action_Value") // Replace with actual value
    {
        reasonForActionControl.addOption({ text: "Exceptional Performance", value: 1 });
        reasonForActionControl.addOption({ text: "Extra Effort", value: 2 });
    }
    else if(actionType === "Negative_Action_Value") // Replace with actual value
    {
        reasonForActionControl.addOption({ text: "Policy Violation", value: 3 });
        reasonForActionControl.addOption({ text: "Performance Issue", value: 4 });
    }
}


Conclusion

This dynamic filtering enhances user experience and data accuracy in Dynamics 365 forms, ensuring that users select only relevant options based on the context. It's a simple yet effective way to implement conditional logic in forms.

No comments:

Post a Comment