The Zurich release has arrived! Interested in new features and functionalities? Click here for more

script to check if a variable exists

Aka Guglielmo
ServiceNow Employee
ServiceNow Employee

Do you have any idea on how to check, inside a workflow, if a variable exists?

Let me say, something like: if (current.variables.variableName.exists).

Because if (current.variables.variableName != '') returns an error.

Thanks

1 ACCEPTED SOLUTION

I suppose an even easier way is just to test it.



if (current.variables.var_name) {


        // The variable exists and is not null


} else {


        // The variable does not exist


}



The same can be done with fields as well to avoid null pointer exceptions. See the best practices.



Reference:


ServiceNow Wiki: Technical Best Practices


View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi William,



Just check using JSUtil nil() method.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I suppose an even easier way is just to test it.



if (current.variables.var_name) {


        // The variable exists and is not null


} else {


        // The variable does not exist


}



The same can be done with fields as well to avoid null pointer exceptions. See the best practices.



Reference:


ServiceNow Wiki: Technical Best Practices


thanks chuck!


You are welcome. Thanks for using the community!