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

epam
Kilo Guru

Hi,



you should add a condition to check if the new value of the field is equal to previous one:



if (newValue == oldValue) {


          return;


    }


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.


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 ---------------




}


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


  }


}


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!