Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

OnChange Catalog Clent Script

ID NOBLE
Tera Expert

Please I need help to resolve the following issue. We have OnChange Client Script that sets the value of NETWORK and MANUFACTURER ID fields when certain values are selected for Category and Subcategory fields respectively. It is actually working, but each time we change the Category and the subcategory fields to something different, the value of NETWORK and MANUFACTURER ID fields won't change. Your helps will be appreciated.

Thanks.

 

Below is the OnChange Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearValue('manufacturer_id');
var cat=g_form.getValue('category');
if(newValue=="sharepoint" && cat=="hardware")
{
g_form.setValue('manufacturer_id','N/A');
g_form.setValue('network','standalone');
}
//Type appropriate comment here, and begin script below

}

1 ACCEPTED SOLUTION

Allen Andreas
Tera Patron

Hi,

Your onChange client script, the one you posted above, will only work on ONE of the two fields that are involved here. Most likely, based off the script, you have the onChange client script only working on the subcategory field.

This means, if you change the category field, this onChange client script won't fire. You'd have to create one for onChange of category and one for onChange of subcategory. The "newValue" used in those client scripts refers to the newValue of the variable/field the onChange client script is set to run on. Meaning, if client script A is onChange of the category, then "newValue" is going to contain the new...value...of the category field.

The manufacturer_id and network fields will only change if the if statement before it....is true for both conditions. So you'd want to review that and ensure BOTH are true for the values of the manufacturer_id and network fields to be changed. Then also double-check your values to ensure N/A and standalone are appropriate values (they may be, but I just wanted to mention that you should check that).

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

15 REPLIES 15

Tony Chatfield1
Kilo Patron

Hi, I am not sure if I have understood your post, as your code shows only 1 conditional check and only sets 1 set of values.

You indicate that the script sets these values when the conditions are met?
if(newValue=="sharepoint" && cat=="hardware")

So it works?
meaning that the reason changing the category again results in no other updates\changes,
appears to be that you have no other conditional check, iE set no other values in your script?
Also I would not clear a form value if you are going to set it in the script, as it is not necessary.

Perhaps try something like

//g_form.clearValue('manufacturer_id');
var cat=g_form.getValue('category');

if(newValue=="sharepoint" && cat=="hardware")
{
g_form.setValue('manufacturer_id','N/A');
g_form.setValue('network','standalone');

} else if(newValue=="sharepoint" && cat=="somethingElse") {

g_form.setValue('manufacturer_id','aNewValue');
g_form.setValue('network','thisHasChanged');

}

 

Thank you Tony.

For clarification, the condition is that if category is HARDWARE and subcategory is SHAREPOINT, then make manufacturer_id equals to "N/A" and at the same time, make network equals to "standalone". Does the above explanation make it more understandable please?

Allen Andreas
Tera Patron

Hi,

Your onChange client script, the one you posted above, will only work on ONE of the two fields that are involved here. Most likely, based off the script, you have the onChange client script only working on the subcategory field.

This means, if you change the category field, this onChange client script won't fire. You'd have to create one for onChange of category and one for onChange of subcategory. The "newValue" used in those client scripts refers to the newValue of the variable/field the onChange client script is set to run on. Meaning, if client script A is onChange of the category, then "newValue" is going to contain the new...value...of the category field.

The manufacturer_id and network fields will only change if the if statement before it....is true for both conditions. So you'd want to review that and ensure BOTH are true for the values of the manufacturer_id and network fields to be changed. Then also double-check your values to ensure N/A and standalone are appropriate values (they may be, but I just wanted to mention that you should check that).

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!