10 Must-Know Tips for Better Scripting in ServiceNow
Struggling with ServiceNow scripting?
Or maybe do you wanna learn something possibly new ?
Efficiency is key in today's fast-paced tech world.
This carousel offers you 10 must-know tips to master scripting in ServiceNow, all aimed at making your code cleaner, faster, and more reliable.
Don't miss out!
Typos are really common bugs.
Autocompletion saves time and minimizes typos, helping you to code more efficiently and accurately.
When you try to use variable you defined always make sure to use ctrl + space and then hit enter to use.
Example:
Do it in order to see:
- Records (use case: check sys_id or copy encoded query)
- Columns (use case: to make sure that the column’s name is correct)
Example:
Second parameter in gs.log() is source.
How to use:
“gs.log(’value is:” + value, ‘lukasz_log’);
Logging with a source makes it easier to filter logs and debug issues.
Example:
Converting an object to a string representation aids debugging and logging.
Use whenever you need to log or examine an object.
When you try to gs.log object without stringify it will print [object Object] which doesn’t give you much.
Lets see it in an example:
gs.log(obj):
But when you use JSON.stringify(obj) it’s much better:
gs.log(JSON.stringify(obj):
Converting a string into an object allows you to manipulate it programmatically.
When you have received data as a string and need to work with it as an object use JSON.PARSE().
JSON.PARSE() and JSON.STRINGIFY() are essential when working with object
(For example in scripts that handle integration)
Using .get() is the most efficient way to fetch a single, unique record.
When you know the sys_id or other unique identifier (like Incident number) and only need one record.
Example:
Knowing the type of your variables is crucial for type-specific operations and debugging.
Use this especially before performing operations that are type-sensitive.
From experience I can tell that significant amount of errors is because of operations on wrong type of variable.
As you can see addition didn’t go as we would expect because of type difference:
Here are logs to better illustrate:
Visually constructing a query ensures accuracy and can be directly copied into your script.
Use whenever you are unsure of the syntax or complexity of a query.
How?
Use the table's UI to construct your query, then right-click and choose ‘Copy Query’.
First I constructed the query, then i will copy and paste it into my Script’s GlideRecord’s Encoded Query:
It centralizes configuration and makes maintenance easier.
Never hardcode sys_id !
Always store sys_id of any record in system properties.
It is way easier to maintain and can be changed by a person that doesn’t code which is a big plus.
First create property which stores sys_id:
Then use it in a script:
Really useful shortcut to make commenting faster:
- Use Ctrl + Space to see the variable names
- Right click on a Table Name when querying
- Use second parameter in gs.log()
- Make Objects Readable with JSON.stringify()
- Parse Strings into Objects with JSON.parse()
- Utilize .get() in GlideRecord
- Know your Data Types with typeof
- Construct and Copy Queries from Tables
- Store sys_id in Script Properties
- Quickly comment by using CTRL + /