The CreatorCon Call for Content is officially open! Get started here.

addOption/removeOption in Client script not working properly

Richard Thomas2
Tera Contributor

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

4 REPLIES 4

Prince Arora
Tera Sage

@Richard Thomas2 ,

 

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.

RubenFuertes
Tera Guru

ServiceNow10sun
Giga Guru

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');

-O-
Kilo Patron

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.