- 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:54 PM
Paste your script with onchange field name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2015 11:30 PM
Here is my script. The basic idea is, I am watching a field waiting for it to change (u_troubleshooting_notes). Once it changes, if a condition is met (length > 999 in this case), I'm injecting a string into the field value, thus changing the field again and increasing it's length. At which point the Client Script sees the change, the internal condition evaluates to true (again), and the script runs (again), putting me in a loop.
I need that last part not to happen.
In case you're wondering, this is all happening on a Record Producer using a Multi Line Text variable. There is no way for me to set a max length on this field type.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var oldFieldValue = g_form.getValue('u_troubleshooting_notes'); //this is the on change field
if (oldFieldValue.length > 999) { //checking length
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); //inserting string to show where the 'cutoff' is
g_form.setValue('u_troubleshooting_notes',newFieldValue); //setting value again
return;
}
//the onChange field is u_troubleshooting_notes
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2015 11:41 PM
In if condition you can try newValue.length > 999
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 12:05 AM
Unfortunately that did not help. Adding that condition causes the Client Script to not run at all