How can I stop an onChange client script from running when the field is populated/changed by a template?

qwertyflop
Giga Guru

I have some checks that run on a list collector field when it changes. I do not want these checks to run when the field is set by a template because when there are multiple items selected for those list fields it results in way too many messages appearing. How can I stop the script from running when it is changed by a template?

I found an old post that essentially recommends adding a u_template field to the form and setting that in the template. I am curious if there is a way to dynamically set that u_template field with the actual name of the template, so that I don't have to require users to enter the name twice in the template to prevent those messages from appearing.

https://community.servicenow.com/community?id=community_question&sys_id=05e14ba9db98dbc01dcaf3231f9619cd

 

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

Unfortunately, there isn't a trigger you can tap in to to look at the field change and see if it's a template or not. You can remove the behavior from happening when the field value becomes 'x' (which would always be the case when using a template). That's about it.

The onChange scripts are going to process the moment the field value changes.

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


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

-O-
Kilo Patron
Kilo Patron

This is natively supported by the platform UI - ignore me if you are talking about something else.

The default template for onChange client scripts is:

function onChange (control, oldValue, newValue, isLoading, isTemplate) {
...

Parameter isTemplate will be true if the change happened because a template has been applied and will be false in other cases.

B.t.w. there is another similar check that can be done by calling:

g_form.isLiveUpdating()

it will tell if the change happened because the record has been updated from another session and the value changed because of that.

All in all you just need to check variable isTemplate that it is not true and exit the onChange script if it is.