Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

remove newValue ==

Community Alums
Not applicable

Hi,

For the onChange client script, for which scenarios, do we remove "newValue ==" in the if condition? we use 

if ( isLoading) {

      return;

          }

instead.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if ( isLoading || newValue == '') {

      return;

          }

}

 

Regards

Suman P.

3 REPLIES 3

Clara Lemos
Mega Sage

Hi @Community Alums ,

 

I remove the :  newValue=='  '   from the condition when I need my new value to be empty for the script to run.

 

For example, if a need to clear a field value when another field value changes to empty. 

 

Let's suppose that your Sold Product value depends on the Consumer value.

If your Consumer values changes to empty, you'll want your Sold Product value to be clear. 

If you keep the newValue==' ' in your condition you'll not be able to run a line of code to clear the sold product value, because it will return first.

When actually what you are looking for is :

  if (newValue === null || newValue === '') {
        g_form.clearValue('sold_product');
        return;
    }

 

To summarise, if you need  something to be done when your new value is empty (when you clear that field value) you should modify the OOTB condition. 

 

If that helps please mark my answer as correct / helpful!
And if further help is needed please let me know

Cheers

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Danish Bhairag2
Tera Sage

Hi @Community Alums ,

 

In ServiceNow's client-side scripting, the `onChange` client script runs whenever a field value changes. The `newValue` check is typically used to perform actions when a specific value is set. However, the condition you've provided with `isLoading` is used to prevent the script from running during the initial load of the form.

 

If you remove the `newValue ==` check and solely rely on `isLoading`, it means that your script will run whenever the field changes, regardless of the specific new value. This could be useful if you want the script to perform some action whenever the user interacts with the field, irrespective of the value they set.

 

For example, if you have logic that needs to execute whenever the field is modified, regardless of the specific value, you might use:

 

 

if (isLoading) {

    return;

}

 

// Your logic here

 

 

On the other hand, if you want to trigger the script only when a particular value is set, you would use:

 

 

if (isLoading || newValue != 'desiredValue') {

    return;

}

 

// Your logic here

 

 

Remember to choose the approach that aligns with your specific requirements and use case.

 

Thanks,

Danish

 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Community Alums ,
I trust you are doing great.

In this script, the if (isLoading || newValue == '') condition serves two purposes:

  1. isLoading: This check is used to determine if the script is being executed as a part of the form loading process. When a form is initially loaded, all onChange scripts are executed once. This check prevents the script from running during this initial load, as typically, you'd want it to run only in response to a user's action.

  2. newValue == '': This check is used to determine if the new value of the field is empty (''). Depending on the scenario, you might want to exit the function if the field has been cleared.

There are few scenarios where you might want to remove the newValue == '' conditions


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi