What is the best approach to declare constants in an application?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 05:53 AM
I was wondering where is the best place and what is the best approach to declare constants i.e variables in an app that never change. What i have done is to use a script include however I suspect there must be a better and clearer approach. I've included a snippet of my code and would like to know what the best practice for dealing with constants...
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 05:56 AM
That works! You're on the right track. Nice looking code. A few comments and you'll be a pro.
Since your phrase "never change" always invites risk... You could consider using properties as well. For example:
this.daysToExpire = gs.getProperty('myapp.days.to.expire', 7); // get the number of days from a property
http://wiki.servicenow.com/index.php?title=System_Properties_Best_Practices
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 10:40 PM
I think the system properties is a good approach for what i'm looking to do ctomasi . With my approach I have to continuously create an object to get access to my constant values, however with the system properties approach, i can simply access them once they have been defined.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2016 07:37 AM
Great idea. I typically use system properties for numerical values, true/false values, group names, etc.
The things I put in the initialize() function tend to be table names, for example, whether or not I'm going to use debugging (based on a property), etc.
this.requestTable = 'u_my_request';
this.debug = (gs.getProperty('u_my_debug', 'false') == 'true);
I've gone back years later and manually changed my app from global to scoped, which definitely requires a visit to the dev system, and it sure is handy to just change your table name once at the top and have faith that it's fixed all the way through the script include and anyone else who calls it!
The thing to remember about properties is that they are ALL strings. Even the true/false ones are strings! In this scenario...
this.debug = gs.getProperty('u_my_debug'); // Results in 'true' or 'false'
All your comparisons would have to be:
if (this.debug == 'true)
gs.debug('>>>DEBUG: Starting to debug');
Me? I like to use boolean variables as booleans. I say,
if (this.debug)
gs.debug('>>>DEBUG: Starting to debug');
Which is why I did the more complex version in the first code snippet. It sets a default of false if there is no property and does a comparison to the string 'true'. If it is a match, then the boolean is true, else false. Pretty tight!
Anyway, I hope that's helpful. These are the kind of sidebar discussions I have during break on those rare occasions where I'm invited to teach a class or workshop.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2016 03:04 PM
Let me know if I have answered your question. If so, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this discussion from your "inbox", use the "mark as correct" option under actions. If you are viewing it directly from the thread use the Correct Answer link (red with a star).
Thank you