The CreatorCon Call for Content is officially open! Get started here.

Dynamically add/activate options in a dropdown box

Kamva
Giga Guru

Hi devs,

 

I want to dynamically add or active options in a dropdown field a pacticular value part of the selection in dropdown in a multi-selection field. In attempting to attain this requirement, I have tried a business rule and a client script. I am not winning with both. Below is my client script configuration in trying to attain the requirement.

Multi-selection field:

Kamva_1-1724051574012.png

When Container Platform is part of the selection in this field, I want to add or activate options on the dropdown box.

 

 Client script configurations:

Kamva_2-1724051892475.png

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Check if "Container Platform" is selected in the multi-selector field
    var selectedValues = g_form.getValue('busine_service').split(',');
    if (selectedValues.indexOf('Container Platform') !== -1) {
        
        // Activate "Infrastructure" and "Application" options in the category dropdown
        activateCategoryOptions(['Infrastructure', 'Application']);
    }
}

// Function to activate specific category options in the dropdown
function activateCategoryOptions(categoriesToActivate) {
    var categoryField = g_form.getControl('category');
    var options = categoryField.options;

    // Iterate through dropdown options and activate the specified ones
    for (var i = 0; i < options.length; i++) {
        var optionValue = options[i].value;
        
        if (categoriesToActivate.indexOf(optionValue) !== -1) {
            options[i].disabled = false;
            options[i].style.display = '';
        } else {
            // Optionally, disable or hide other options
            options[i].disabled = true;
            options[i].style.display = 'none';
        }
    }
}


I am happy to answer any questions.

 

Thanks in advance,

Kamva

5 REPLIES 5

Ravi Gaurav
Giga Sage
Giga Sage

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

// Check if "Container Platform" is selected in the multi-selector field
var selectedValues = g_form.getValue('busine_service').split(',');

if (selectedValues.indexOf('Container Platform') !== -1) {
// Activate "Infrastructure" and "Application" options in the category dropdown
activateCategoryOptions(['Infrastructure', 'Application']);
} else {
// If "Container Platform" is not selected, reset the dropdown to its original state
resetCategoryOptions();
}
}

// Function to activate specific category options in the dropdown
function activateCategoryOptions(categoriesToActivate) {
var categoryField = g_form.getControl('category');
var options = categoryField.options;

// Iterate through dropdown options and activate or disable them
for (var i = 0; i < options.length; i++) {
var optionValue = options[i].value;

if (categoriesToActivate.indexOf(optionValue) !== -1) {
// Enable the specific options
options[i].disabled = false;
options[i].style.display = '';
} else {
// Optionally, disable or hide other options
options[i].disabled = true;
options[i].style.display = 'none';
}
}
}

// Function to reset the dropdown to its original state
function resetCategoryOptions() {
var categoryField = g_form.getControl('category');
var options = categoryField.options;

// Iterate through dropdown options and reset their state
for (var i = 0; i < options.length; i++) {
options[i].disabled = false;
options[i].style.display = '';
}
}

can you try this ?

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/