The CreatorCon Call for Content is officially open! Get started here.

Prevent onChange Client Script from running more than once

Matthew Glenn
Kilo Sage

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?

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

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


  }


}


View solution in original post

9 REPLIES 9

Ashish Kumar Ag
Kilo Guru

Paste your script with onchange field name


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


}


In if condition you can try newValue.length > 999


Unfortunately that did not help. Adding that condition causes the Client Script to not run at all