ServiceNow Survival Guide #4 – Scripting with Constants

Previously, we talked about how you could expand the capabilities of your script includes using extensions of script includes. Our example we used was EventUtil and EventUtilBase.

For a refresher you can read the blog post again here – ServiceNow Survival Guide #3

This time, we are going to talk about Constants, which can be used as a means to set up many variables at once to be used across multiple script includes, kept together in one location.

You may be asking “but isn’t this the same as using system properties?”

It is similar, but it offers a little bit more control.

System properties can be grouped together, but not quite as cleanly as constants. Constants can also make calls to system properties. Think of this as more of an extension of system properties.

Below is a snippet of the BCPConstants script include, found in the BCM application.

There are a few things to note here:

  • The BCPConstants script include doesn’t have any functions in it besides the initialize.
  • The BCPConstants.TABLES is located outside the standard usage of a script include.
  • With BCPConstants.TABLES being outside, it is treated as an extra piece of functionality that we can utilize, but without needing to do the same method of calling to it.
  • We call to the constant by doing the following
    • this.BCP_T = sn_bcp.BCPConstants.TABLES;
      • By doing the above, this.BCP_T will have access to all the values in the BCPConstants.TABLES object.

How does this help you with coding more efficiently?

Well, you’ll be able to keep all your variables in one place. You could even use them with System Properties. This prevents the need of calling the same system property or variable in multiple places when it shouldn’t ever change. This also keeps all of your values in one spot, so if a major change does happen (such as a state changing values) it would be easier to adjust from this single point.

Tables are a good example of value that shouldn’t change, as not all tables have the same fields available to them.

Another great example is states. It is very common to need to build scripts around states changing or being set to a specific value. If using constants, you would be able to refer to the constant and it would use that value.

Related Posts

About Us

Our team is focused on helping our customers realize the full potential of the ServiceNow platform through flexibility, transparency, and technical leadership.

Let’s Socialize

Popular Post