Arrays vs Sets in ServiceNow Scripting: Which One Should You Choose?

Amitoj Wadhera
Kilo Sage

In ServiceNow scripting, making the right choice between arrays and sets can mean the difference between a smooth-running script and a headache-inducing one. Both data structures have their strengths and weaknesses, and knowing when to use each can help you write more efficient and effective code. Let’s dive into the details!

 

Note: Before proceeding further, it's important to mention that Sets were introduced in ECMAScript 6 (ES6). Therefore, they are only applicable in scoped applications that support ECMAScript 6 (ES6) and above.

 

Arrays

Think of arrays as your trusty sidekick when it comes to organising data in ServiceNow. They're like numbered lists that can hold all sorts of stuff – from incident IDs to user names and everything in between. With arrays, you can keep things in order, access them by their position, and even have duplicates if needed.

When to Reach for Arrays:

  1. Ordered Data: If you need to keep things in a particular order – like processing incidents in the order they were created – arrays are your go-to choice.
  2. Easy Access: When you find yourself reaching for data by its position in the list, arrays make it a breeze.
  3. Duplicates Welcome: Need to store the same value multiple times? No problem! Arrays are happy to oblige.

Real-World Example: Let's say you're writing a script to process a bunch of incidents in the order they were created. An array would be perfect for this job because it keeps things tidy and lets you access each incident in turn.

 

 

var incidentIds = [];
var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
    incidentIds.push(gr.getValue('sys_id'));
}
incidentIds.forEach(function(id) {
    gs.info('Processing incident: ' + id);
});

 

 

 

Sets

Now, let's talk about sets – the neat freaks of the data world. Sets are all about keeping things unique and tidy. They don't care about order – they just want to make sure there are no duplicates hanging around. If you need to compile a list of unique values, sets are your best friends.

When to Choose Sets:

  1. Unique Values Only: When you need to ensure there are no duplicates in your collection, sets have got you covered.
  2. Quick Checks: If you find yourself constantly asking, "Is this value already in the list?" sets can give you a lightning-fast answer.
  3. Order Be Damned: Who needs order anyway? Sets don't care about the order of things – they just want to keep it clean.

Real-World Example: Imagine you're tracking user access to a particular application. You want to compile a list of unique user IDs without any duplicates. A set is the perfect tool for this job because it automatically keeps things neat and tidy for you.

 

 

var userIds = new Set();
var gr = new GlideRecord('syslog');
gr.addQuery('application', 'my_application');
gr.query();
while (gr.next()) {
    userIds.add(gr.getValue('user_id'));
}
userIds.forEach(function(id) {
    gs.info('User accessed the application: ' + id);
});

 

 

 

Choosing Wisely: When to Use Arrays or Sets

So, when should you use arrays, and when should you reach for sets? Here's a quick cheat sheet to help you decide:

Use Case Array Set

Ordered DataYesNot a priority
Duplicate ValuesSure thingOne and only
Quick Existence CheckNot the bestLightning fast

 

Conclusion: Finding Harmony in Your Scripts

In the end, choosing between arrays and sets in ServiceNow scripting boils down to your specific needs. Arrays are great for keeping things in order and allowing duplicates, while sets shine when you need to ensure uniqueness and fast existence checks. By understanding the strengths of each data structure, you can write cleaner, more efficient scripts that get the job done without breaking a sweat.

 

 

If this helped, could you please mark it as helpful?

Thank you! Your feedback helps us know we're on the right track!

Amitoj Wadhera

 

1 REPLY 1

PrashantLearnIT
Giga Sage

Nice Information.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************