addOption/removeOption in Client script not working properly

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 02:09 AM
Hi Experts,
I have this script - I just want 1 option removed and 2 options added when the 'HR Service' Reference field is disciplinary.
// This script will run when the 'hr_service' field is changed on an 'HR Employee Relations Case' record
// and the new value is 'Disciplinary'
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Check if the new value of 'hr_service' field is 'Disciplinary'
if (newValue == 'disciplinary') {
// Remove the option 'No Formal Action' from the 'outcome' field
g_form.removeOption('outcome', 'no_formal_action');
// Add the options 'NFA – Informal Management' and 'NFA – No case' to the 'outcome' field
g_form.addOption('outcome', 'nfa_informal_management', 'NFA – Informal Management',9);
g_form.addOption('outcome', 'nfa_no_case', 'NFA – No case',10);
}
}
I cant get it firing! Can anyone advise?
Thanks as always!
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 02:28 AM
Few things I want to check with you:
1) Can you please add a alert message of newValue in the script as:
alert(newValue) // to check what is coming in the newValue
2) Try updating the if condition as "if (newValue+"" == 'disciplinary')";
3) if it still doesn't solve your query, what is the data type of "outcome" field, pls check whether you have added a correct backend name of "outcome" field.
updated code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert(newValue)
// Check if the new value of 'hr_service' field is 'Disciplinary'
if (newValue + "" == 'disciplinary') {
// Remove the option 'No Formal Action' from the 'outcome' field
g_form.removeOption('outcome', 'no_formal_action'); // check you have used correct name of backend field
// Add the options 'NFA – Informal Management' and 'NFA – No case' to the 'outcome' field
g_form.addOption('outcome', 'nfa_informal_management', 'NFA – Informal Management',9);
g_form.addOption('outcome', 'nfa_no_case', 'NFA – No case',10);
}
}
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 08:29 AM
What release are you using?
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1001598
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 09:37 AM
Hi @Richard Thomas2 ,
I am not sure why you have given the numeric values in the addOption syntax , just try the below lines replaced and it should work and make sure the field outcome is a choice field as this AddOption and Remove option works only when the field type is Choice.
g_form.addOption('outcome', 'nfa_informal_management', 'NFA – Informal Management');
g_form.addOption('outcome', 'nfa_no_case', 'NFA – No case');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 04:40 PM
Considering what you say:
'HR Service' Reference field is disciplinary
code
if (newValue + "" == 'disciplinary') {
is 99.(9)% wrong.
Reference fields will store GUID like, not human readable values; e.g. something like a44506db0715f1503c7bf4be7c1ed07b.
In light of the above, as others said, the values of choices might also not be OK; you should also post a screen-shot of the list of choices where column Value is visible to make sure the code is OK.
Of course the better solution would be to use field dependency and choice scripts - which work kinda' like reference qualifiers.