Dynamic Choice Management in Dynamics 365: Adding and Removing Options on the Fly

“Change is the only constant,” and this couldn’t be truer when it comes to managing choice fields in Dynamics 365. Whether adapting to new business requirements or just tweaking forms to enhance user experience, Dynamics 365 developers frequently need to modify choice fields dynamically. This blog dives into two potent JavaScript API methods: addOption and removeOption, which I’ve been leveraging in my recent projects to dynamically manage choice options on forms.


1. Dynamically Adding Options with addOption

The addOption method allows developers to add new options to a choice field programmatically. This is particularly useful when the options in a choice field need to reflect changes in data that aren’t static or predefined.

Imagine a scenario in an eCommerce system where new product categories are added frequently. Administrators can dynamically add these new categories to the product category choice field without needing a system customization request.

Code Example:

function addNewCategory() {
    var newOption = {
        text: "New Category",
        value: 104
    };
    formContext.getControl("new_productcategory").addOption(newOption, 3);
}

“The more the merrier!” This method lets your choice fields keep up with your ever-expanding business vocabulary without breaking a sweat.


2. Streamlining UI with removeOption

Conversely, the removeOption method is used to remove options from a choice field. This helps in maintaining a clean and relevant user interface, especially when certain options become obsolete or are seasonally irrelevant.

Consider a booking system where certain tour packages are only available seasonally. Removing these options off-season simplifies the choices and enhances user experience by preventing selection errors.

Code Example:

function removeSeasonalOption() {
    var seasonalOptionValue = 105; // Value of the seasonal tour package
    formContext.getControl("new_tourpackage").removeOption(seasonalOptionValue);
}

“Simplicity is the ultimate sophistication.” – Leonardo da Vinci. This method embodies this by letting you simplify the choices as and when needed.


Have you ever found yourself in need of dynamically changing options in your forms? How do you manage to keep your choice fields relevant? Share your experiences or drop any questions in the comments below. Let’s discuss the dynamic world of choice management in Dynamics 365!

Leave a comment