Add option & remove option in Onchange client script

SNOW32
Giga Expert

Hi All,

I am trying addOption & removeOption for selection of resolution category if u_function field change. elseif option is not working, choices are incorrect

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var func = g_form.getValue('u_function');

if(func == 'cad'){

g_form.addOption('u_resolution_category','user_error');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue');
g_form.removeOption('u_resolution_category','application-bug');

}

else if(func == 'it') { 
g_form.addOption('u_resolution_category','application-bug');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue');
g_form.removeOption('u_resolution_category','user_error');

//Type appropriate comment here, and begin script below

}
else {
g_form.addOption('u_resolution_category','issue');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','application-bug');
g_form.removeOption('u_resolution_category','user_error');
}
}

When I choose the "IT" option which matches the elseif condition,but the resolution category choices are removing all options.

Please help me where I am wrong

thanks,

Swetha

 

 

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Can you try replacing else-if with below.

else if(func == 'it') { alert('Inside else if');
g_form.addOption('u_resolution_category','application-bug');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue','Issue'); //where Issue is choice label
g_form.removeOption('u_resolution_category','user_error','User Error'); //where User Error is choice label

//Type appropriate comment here, and begin script below

}

View solution in original post

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Can you try replacing else-if with below.

else if(func == 'it') { alert('Inside else if');
g_form.addOption('u_resolution_category','application-bug');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue','Issue'); //where Issue is choice label
g_form.removeOption('u_resolution_category','user_error','User Error'); //where User Error is choice label

//Type appropriate comment here, and begin script below

}

Thank you, I kept label for addOption & removeOption for all and its working fine.

Shruti
Mega Sage
Mega Sage

Hi,

Can you please try below code  (using g_form.clearOptions in if-else loop)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var func = g_form.getValue('u_function');

if(func == 'cad'){

 g_form.clearOptions('u_resolution_category');

g_form.addOption('u_resolution_category','user_error');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue');
g_form.removeOption('u_resolution_category','application-bug');

}

else if(func == 'it') { 

g_form.clearOptions('u_resolution_category');
g_form.addOption('u_resolution_category','application-bug');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','issue');
g_form.removeOption('u_resolution_category','user_error');

//Type appropriate comment here, and begin script below

}
else {

g_form.clearOptions('u_resolution_category');
g_form.addOption('u_resolution_category','issue');
g_form.addOption('u_resolution_category','other');

g_form.removeOption('u_resolution_category','application-bug');
g_form.removeOption('u_resolution_category','user_error');
}
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this

1) Ensure you clear the options when onChange triggers

2) then add only those options you require to be seen

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var func = g_form.getValue('u_function');

g_form.clearOptions('u_resolution_category');

if(func == 'cad'){

g_form.addOption('u_resolution_category','user_error');
g_form.addOption('u_resolution_category','other');

}

else if(func == 'it') { 
g_form.addOption('u_resolution_category','application-bug');
g_form.addOption('u_resolution_category','other');

//Type appropriate comment here, and begin script below

}
else {
g_form.addOption('u_resolution_category','issue');
g_form.addOption('u_resolution_category','other');
}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader