- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2015 10:38 PM
I have an onChange Client Script that, if conditions within the script are met, changes the value of the field that the onChange script is triggered by. This unfortunately causes an in-escapable loop that I can't seem to break out of.
if possible, I'd like the onChange script to run the first time as expected, but than disable it after that first run unless the form is reloaded.
Is this doable?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 12:19 AM
Not the best way but will work..
var isTroubleShooted=true;
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var oldFieldValue = g_form.getValue('u_troubleshooting_notes');
if (oldFieldValue.length > 1000 && isTroubleShooted) {
alert("Troubleshooting Notes field is too long. Results will be truncated after the '<-- -->' that ServiceNow just added to the field. Please adjust the notes field so that it is less than 999 characters");
var newFieldValue = oldFieldValue.slice(0,999) + " <-- --> " + oldFieldValue.slice(1000);
isTroubleShooted=false;
g_form.setValue('u_troubleshooting_notes',newFieldValue);
setTimeout(function(){
isTroubleShooted = true;
}, 1000); // restore functionality after 3 seconds
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2015 10:58 PM
Hi,
you should add a condition to check if the new value of the field is equal to previous one:
if (newValue == oldValue) {
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2015 11:34 PM
I don't know if that will work for me, as my Client Script itself is also changing the value of the field that it is watching (creating my loop).
So when it runs, the old value and new value will always be different.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 12:04 AM
Oh, well, so you can use an additional variable to store a new value or just flag the field is already changes.
var is_changed = false;
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '' || is_changed) {
return;
}
is_changed = true;
// ------------ lets change the field ---------------
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 12:19 AM
Not the best way but will work..
var isTroubleShooted=true;
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var oldFieldValue = g_form.getValue('u_troubleshooting_notes');
if (oldFieldValue.length > 1000 && isTroubleShooted) {
alert("Troubleshooting Notes field is too long. Results will be truncated after the '<-- -->' that ServiceNow just added to the field. Please adjust the notes field so that it is less than 999 characters");
var newFieldValue = oldFieldValue.slice(0,999) + " <-- --> " + oldFieldValue.slice(1000);
isTroubleShooted=false;
g_form.setValue('u_troubleshooting_notes',newFieldValue);
setTimeout(function(){
isTroubleShooted = true;
}, 1000); // restore functionality after 3 seconds
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 08:31 AM
That did it. Did exactly what I needed.
As you said, there may have been a cleaner or more graceful way to do it but I couldn't figure it out. This will do just fine.
Thank you very much!