Catalog client script to control which variable choice options are displayed

jonathangilbert
Mega Sage

Hi

I wish to control which choice options are displayed in a Variable based on the response of another variable. I have seen on the forums it is controlled by a On Change client script using addOption, or removeOption.

I have created the client script which works when the first variable value is changed, the second variable only displays the choice options I configured, however if the value of the first variable is changed again, the second variable choice options are incorrect, it does not refresh.

 

What am I missing in my script to ensure options are always updated based on variable 1 being changed. Here is my script

 

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

if (newValue == 'Default') {// shows all choice options except the admin role 13587b75-c4d1-4a07-8641-9d00693b1f65
g_form.removeOption('which_role_s_do_you_require', '13587b75-c4d1-4a07-8641-9d00693b1f65');
}

 if (newValue == 'Privileged') {// only displays the admin role 13587b75-c4d1-4a07-8641-9d00693b1f65
g_form.removeOption('which_role_s_do_you_require', 'd74c4943-4347-4340-9a61-fc85ccce3ef7');
g_form.removeOption('which_role_s_do_you_require', 'a3f647e5-59b4-43c3-b05d-48ec2604d692');
g_form.removeOption('which_role_s_do_you_require', '26687442-3860-419c-bc96-4069bcca3dc1');
g_form.removeOption('which_role_s_do_you_require', '7de36bf0-828a-48a8-a31e-675f1e3983c7');
g_form.removeOption('which_role_s_do_you_require', 'e69af349-fb1b-4cf3-becd-d9e6a8ae8bd4');
g_form.removeOption('which_role_s_do_you_require', 'ae557f87-0a3d-47f0-91de-668de79e31e5');
g_form.removeOption('which_role_s_do_you_require', 'd5baf8dd-0fee-4d76-82c0-f884c198b4dd');
g_form.removeOption('which_role_s_do_you_require', '43babf7d-6eb0-42e4-9dcd-95f19e93b922');
g_form.removeOption('which_role_s_do_you_require', 'd63088a0-335b-41ba-8450-bfd1c70ef85b');
g_form.removeOption('which_role_s_do_you_require', '2d9202df-3977-493f-b354-ff9e05396583');
g_form.removeOption('which_role_s_do_you_require', '5fbeb849-d34c-4a0b-9d28-fa23a2c41c57');
g_form.removeOption('which_role_s_do_you_require', '82d368ef-bf79-4dd0-af2f-d8c68b2b1ff6');
}}
1 ACCEPTED SOLUTION

@jonathangilbert ,

g_form.addOption(fieldName, choiceValue, choiceLabel, choiceIndex) method, the mandatory and optional parameters are split as follows:
here Choice Label is also mandatory along with the backend value of the choice
Mandatory Parameters
  • fieldName: The exact backend column name of your choice list or select box.
  • choiceValue: The actual value stored in the database when this option is selected.
  • choiceLabel: The user-friendly text that appears inside the dropdown menu
function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        g_form.clearOptions('which_role_s_do_you_require');
        return;
    }



    if (newValue == 'Default') { // shows all choice options except the admin role 13587b75-c4d1-4a07-8641-9d00693b1f65
        g_form.clearOptions('which_role_s_do_you_require');
        g_form.addOption('which_role_s_do_you_require', '', '-- None --');
        g_form.addOption('which_role_s_do_you_require', 'd74c4943-4347-4340-9a61-fc85ccce3ef7','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', 'a3f647e5-59b4-43c3-b05d-48ec2604d692','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '26687442-3860-419c-bc96-4069bcca3dc1','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '7de36bf0-828a-48a8-a31e-675f1e3983c7','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', 'e69af349-fb1b-4cf3-becd-d9e6a8ae8bd4','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', 'ae557f87-0a3d-47f0-91de-668de79e31e5','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', 'd5baf8dd-0fee-4d76-82c0-f884c198b4dd','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '43babf7d-6eb0-42e4-9dcd-95f19e93b922','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', 'd63088a0-335b-41ba-8450-bfd1c70ef85b','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '2d9202df-3977-493f-b354-ff9e05396583','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '5fbeb849-d34c-4a0b-9d28-fa23a2c41c57','Label that need to be display');

        g_form.addOption('which_role_s_do_you_require', '82d368ef-bf79-4dd0-af2f-d8c68b2b1ff6','Label that need to be display');

    }



    if (newValue == 'Privileged') { // only displays the admin role 13587b75-c4d1-4a07-8641-9d00693b1f6
        g_form.clearOptions('which_role_s_do_you_require');
        g_form.addOption('which_role_s_do_you_require', '', '-- None --');

        g_form.addOption('which_role_s_do_you_require', '13587b75-c4d1-4a07-8641-9d00693b1f65','Label that need to be display');

    }
}

Add the labels for all the above options.

If my response helped, mark it as helpful and accept the solution.
Thanks,

Dinesh

View solution in original post

10 REPLIES 10

yashkamde
Giga Sage

Hello @jonathangilbert ,

 

try this script in which label is a required argument. In your second to last version you were only passing fieldName and choiceValue.

 

Also when you clearOptions() and then immediately addOption() a set of real values with nothing blank in the list..

overall script :

function onChange(control, oldValue, newValue, isLoading) {
    var roleVariable = 'which_role_s_do_you_require';

    g_form.clearOptions(roleVariable);

    if (isLoading || newValue == '') {
        return;
    }

    var roleMap = {
        'Privileged': [
            { value: '13587b75-c4d1-4a07-8641-9d00693b1f65', label: 'Admin' }
        ],
        'Default': [
            { value: 'd74c4943-4347-4340-9a61-fc85ccce3ef7', label: 'Role 1' },
            { value: 'a3f647e5-59b4-43c3-b05d-48ec2604d692', label: 'Role 2' },
            { value: '26687442-3860-419c-bc96-4069bcca3dc1', label: 'Role 3' },
            { value: '7de36bf0-828a-48a8-a31e-675f1e3983c7', label: 'Role 4' },
            { value: 'e69af349-fb1b-4cf3-becd-d9e6a8ae8bd4', label: 'Role 5' },
            { value: 'ae557f87-0a3d-47f0-91de-668de79e31e5', label: 'Role 6' },
            { value: 'd5baf8dd-0fee-4d76-82c0-f884c198b4dd', label: 'Role 7' },
            { value: '43babf7d-6eb0-42e4-9dcd-95f19e93b922', label: 'Role 8' },
            { value: 'd63088a0-335b-41ba-8450-bfd1c70ef85b', label: 'Role 9' },
            { value: '2d9202df-3977-493f-b354-ff9e05396583', label: 'Role 10' },
            { value: '5fbeb849-d34c-4a0b-9d28-fa23a2c41c57', label: 'Role 11' },
            { value: '82d368ef-bf79-4dd0-af2f-d8c68b2b1ff6', label: 'Role 12' }
        ]
    };

    var roles = roleMap[newValue] || [];

    // Always seed a blank option so mandatory validation behaves correctly
    g_form.addOption(roleVariable, '', '-- Select a role --');

    for (var i = 0; i < roles.length; i++) {
        g_form.addOption(roleVariable, roles[i].value, roles[i].label);
    }

    // Force the user to reselect rather than leaving a stale/invalid value
    g_form.clearValue(roleVariable);
}

 

If my response helped mark as helpful and accept the solution.