How to limit choice options for choice field?

Community Alums
Not applicable

Hi

I need to write a client script to show only few number of options for category field to a user group. Currently there are about 70 choices in that field

I tried g_form.clearOptions with g_form.addOption 

in an onLoad client script

 

g_form.clearOptions();

g_form.addOption(‘category’, ‘A’,’A’);

g_form.addOption(‘category’, ‘B’,’B’);

g_form.addOption(‘category’, ‘C,’C’);

g_form.addOption(‘category’, ‘D’,’D’);

 

 

But when user selects C and save it shows as A after form refresh. I assume its because of clearoptions method. In list view it shows correctly as C. 
Can someone please help on how to fix it?

 

Thanks

Bimsari

1 REPLY 1

Abhay Kumar1
Giga Sage

@Community Alums Yes that might be causing the issue , there is a better approch to use to achieve same with following scripts :

function onLoad() {

    // Check if the user has a specific role

    if (g_user.hasRole('your_role')) { // Replace 'your_role' with the exact role you want to check

        // Define allowed categories for users with this role

        var allowedCategories = ['A', 'B', 'C', 'D'];

        // Get all available options in the Category field

        var allOptions = g_form.getOptions('category');

        // Iterate over each option and remove those not in the allowed list

        for (var i = 0; i < allOptions.length; i++) {

            var optionValue = allOptions[i].value;

            if (!allowedCategories.includes(optionValue)) {

                g_form.removeOption('category', optionValue);

            }

        }

    }

}

please feel free to modify code as per your requirements.

Hope this will help you.