onChange client script triggered twice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2014 10:11 AM
My script resets the value of the field to null if the server returns a negative result, and it appears that that reset is causing the onChange function to be triggered again. Anyone else have this problem? Any suggestions to resolve?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2014 10:38 AM
Hi Daniel ,
I am assuming that this Client script is onChange of field "u_cost_object". If this is the case then:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading){
return;
}
// below line added to check onChange and newValue is not blank and null
if(!isLoading && newValue != '' || newValue != null){
var costInfo = checkSAPInfo();
var costObject = g_form.getValue('u_cost_object');
var deptID = g_form.getReference('u_requesting_department');
var deptNumber = deptID.name;
if (costObject!='' && deptNumber!=''){
if (costInfo != "true"){
alert("This is not a valid cost object. Please contact your business officer to obtain a valid cost object.");
g_form.setValue('u_cost_object','');
}
else{}
}
}
}
...
function
You can refer Client Scripts - ServiceNow Wiki for more clarity
Please mark answer correct if it was really helpful.
Thanks,
Darshan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2014 10:53 AM
Thanks Darshan,
Unfortunately that doesn't seem to resolve the issue - the checkSAPInfo() function is still being called twice.
Thanks for your help,
Dan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2014 11:44 AM
Hi Daniel,
Minor change in Darshan Rathod's code.
if(!isLoading && newValue != null && newValue != '' ) {
//rest of the code
}
Thanks
Mandar
EDIT : is u_cost_object a boolean field? In that case you can set it to "false" and make changes in above conditions accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2014 11:51 AM
Ah hah! That seems to have done the trick. Thanks so much Mandar!
-Dan