Catalog Client Script onChange - Prevent Loop

stevejarman
Giga Guru

I have a field (FieldD) that is set based on other fields on a form. i.e. their onChange event runs a script that updates the value in FieldD.

FieldA (onChange recalculates value in FieldD)

FieldB (onChange recalculates value in FieldD)

FieldC (onChange recalculates value in FieldD)

FieldD

I now want to also add an onChange event for FieldD that will process things if the user overrides the automatic value by typing something in there directly. The problem is, FieldD's onChange event will fire when the field is changed by the user directly, OR, when onChange events of FieldA, FieldB, or FieldC fire.

Does anyone know of a way to add to FieldD's onChange code to say "Only fire if the change is happening directly in FieldD"?

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

I have done this before by using variable to specify when to ignore FieldD's onchange function.



Create an onLoad Client Script that declares a variable like: var isAutoRecalculating = false; (or something)


In your FieldA-C onChange functions, set isAutoRecalculating to true, then run the recalculation, set the new value of FieldD (this in when the FieldD onChange gets called), then AFTER you set the new value of FieldD, set isAutoRecalculating back to false.


In your FieldD onChange function have a line at the top that says:


if (isAutoRecalculating)


      return;


// Do normal stuff here if not autoRecalculating


View solution in original post

3 REPLIES 3

Geoffrey2
ServiceNow Employee
ServiceNow Employee

I have done this before by using variable to specify when to ignore FieldD's onchange function.



Create an onLoad Client Script that declares a variable like: var isAutoRecalculating = false; (or something)


In your FieldA-C onChange functions, set isAutoRecalculating to true, then run the recalculation, set the new value of FieldD (this in when the FieldD onChange gets called), then AFTER you set the new value of FieldD, set isAutoRecalculating back to false.


In your FieldD onChange function have a line at the top that says:


if (isAutoRecalculating)


      return;


// Do normal stuff here if not autoRecalculating


Perfect! Thanks - didn't think of that


jbruns2019
Giga Contributor

If you are still around, I have similar issue, a single multi-line field, server name per line (users sometimes supply commas at end and I have to reject that)

This code runs fine first time but because line 8 modifies the newvalue, it runs again and because my code adds commas (designed this way) it then barks thinking the user supplied commas.

How do I stop it from running a second time?

The first if is supposed to prevent this but it fails to.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '' || oldValue == newValue){
        return;
    }
    var onecomma = newValue.indexOf(",");
    if ( onecomma < 0 ){
        alert('no comma found ' + onecomma);
        var add_comma = newValue.replace(/\s+/g, ',');
        add_comma = add_comma.replace(/,(\s+)?$/, ''); //remove last comma
        g_form.setValue('hostnames', add_comma);
        return true;
    }else{
        alert('You supplied data with one or more commas, no commas are allowed');
        return false;
    }
}