Remove choices except a few choices

1_DipikaD
Kilo Sage

Hi All,

 

I want to remove all the existing choices from a choice field without removing a few choices(suppose 5) .

 

Thank you

7 REPLIES 7

Hello Dipika,'

 

Instead of doing this on the client side, if you don't want these choices, why have it in first place? Remove it from dictionary altogether. 

So are you saying to inactive the choices manually from dictionary in DEV,QA,UAT and PROD ?

 

If yes , will it be the best practice to do ? I am not sure, please clarify .

depends, do you want these choices or no? If yes, best practise will be to remove the options so it shows the right options only. If there is a use case to remove options temporarily which will be used on some other conditions, you can use the below script - 

 

function onLoad() {
    var fieldName = 'your_field_name';  // Replace with your actual choice field name
    var allowedChoices = ['your_choice_value1', 'your_choice_value2'];  // Replace with the values you want to keep

    var options = g_form.getControl(fieldName).options;

    for (var i = options.length - 1; i >= 0; i--) {
        var optionValue = options[i].value;
        if (allowedChoices.indexOf(optionValue) === -1) {
            g_form.removeOption(fieldName, optionValue);
        }
    }
}