My client script to remove options from a choice list doesn't work as expected

Jerome MAISETTI
Mega Guru

Hello
I added the option "4 - No Impact" with value 4 in the impact choice list.
I would like this option to be removed/added based on certain conditions.

I created the following client script to achieve this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   //Type appropriate comment here, and begin script below
   var contract = g_form.getValue('u_service_contract');

    var serviceContractAjax = new GlideAjax('ServiceContractAjax');
    serviceContractAjax.addParam('sysparm_name', "getCSOCContract");
    serviceContractAjax.addParam('sysparm_service_contract', contract);
    serviceContractAjax.getXMLAnswer(getServiceTypeCallback);

    function getServiceTypeCallback(answer) {
        var getCSOCContract = answer;
        if (getCSOCContract == "false") { // OCD INTL Entity
            g_form.removeOption('impact', 4);
        }
    }
}

It removes it when I meet the condition, but it doesn't add it back when the condition is not matched.
I tried to add

else {

g_form.addOption('impact', 4);

}

but this only adds blank lines but not the expected value..

Thanks

Jérôme

1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

Hi,

Did you try something like below,

g_form.addOption('impact',4, "4 - No Impact"); // this function accepts value and label both

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

5 REPLIES 5

Jerome MAISETTI
Mega Guru

It works !! 😄
Thanks a lot 🙂