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

Hi Kamva,

 

Can you use the below script ?

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var selectedValues = g_form.getValue('busine_service').split(',');
if (selectedValues.indexOf('Container Platform') !== -1) {
activateCategoryOptions(['Infrastructure', 'Application']);
} else {
resetCategoryOptions();
}
}

function activateCategoryOptions(categoriesToActivate) {
g_form.clearOptions('category');
categoriesToActivate.forEach(function(category) {
g_form.addOption('category', category, category);
});
}

function resetCategoryOptions() {
g_form.clearOptions('category');
g_form.addOption('category', '', '-- None --');
g_form.addOption('category', 'other_option1', 'Other Option 1');
g_form.addOption('category', 'other_option2', 'Other Option 2');
}

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


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/

Thanks @Ravi Gaurav,

 

I have tried the script, I am not winning. Also, please take into account that I am only trying to add options, in top of the existing options not to substitute.

Ravi Gaurav
Giga Sage
Giga Sage

--------- Try the below ----------------------

 

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

var selectedValues = g_form.getValue('busine_service').split(',');
if (selectedValues.indexOf('Container Platform') !== -1) {
activateCategoryOptions(['Infrastructure', 'Application']);
}
}

function activateCategoryOptions(categoriesToActivate) {
categoriesToActivate.forEach(function(category) {
if (!optionExists('category', category)) {
g_form.addOption('category', category, category);
}
});
}

function optionExists(fieldName, optionValue) {
var options = g_form.getOptions(fieldName);
for (var i = 0; i < options.length; i++) {
if (options[i].value == optionValue) {
return true;
}
}
return false;
}

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


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/

Thanks @Ravi Gaurav, still no luck