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

_Gaurav
Kilo Sage

Hi Jerome,
First, try to clear all the field's value,s and on the basis of the condition use the addOption method to add the values in the Impact field.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   var contract = g_form.getValue('u_service_contract');

g_form.clearOptions('u_service_contract');

call the Ajax 

if(true){

g_form.addOption('imapct','Your value'); //add all the options to show

}

else {

g_form.addOption('impact', 'Your values'); //add all the option to show

}

Jerome MAISETTI
Mega Guru

Hi Gaurav,
I tried like that, but still, it's adding blank lines instead of the actual values.
Any other ideas ?
Thanks,
Jérôme

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

It works! Thanks!