SumanthDosapati
Mega Sage
Mega Sage

 

🔍 The Unsung APIs of ServiceNow — Ep. 1: ArrayUtil

I often find many ServiceNow developers reinventing the wheel when JavaScript’s default methods don’t play nicely with GlideRecord results, encoded queries, or arrays from the platform. But hidden in plain sight, ServiceNow provides a powerful little helper: the ArrayUtil API.

Let’s shine a spotlight on this underappreciated server-side hero.


🎯 What is ArrayUtil?

ArrayUtil is a server-side utility class that helps manipulate arrays more efficiently within the ServiceNow platform. While it may seem like just another wrapper over native JavaScript functions, it truly excels in handling complex ServiceNow-specific arrays, especially in GlideRecord and GlideElement contexts.


🧪 Real-World Use Cases

1. Remove Duplicates with unique()

ar arr = ['IT', 'HR', 'IT', 'Finance'];
var uniqueArr = new ArrayUtil().unique(arr);  
gs.info(uniqueArr); // ['IT', 'HR', 'Finance']
 

Perfect for cleaning up arrays from reference fields or multi-value variables.


2. Check Membership with contains()

 
var roles = ['admin', 'itil', 'catalog_admin'];
if (new ArrayUtil().contains(roles, 'itil')) {
  gs.info('User has itil role');
}

Much cleaner than writing a custom loop every time.


3. Find Differences with diff()

var currentRoles = ['admin', 'itil'];
var desiredRoles = ['admin', 'itil', 'x_abc_custom'];
var missingRoles = new ArrayUtil().diff(desiredRoles, currentRoles);
gs.info(missingRoles); // ['x_abc_custom']
Helps when reconciling current state vs expected state in business rules or access control logic.

🧠 Other Handy Methods

Method Description
intersect() Returns common elements from two arrays
union() Merges arrays and removes duplicates
join() Joins array elements into a single string
isArray() Validates if an object is an array

🤖 Bonus Tip

Working with GlideElement values like record.variables.multi_select.getDisplayValue()? Often it returns a comma-separated string. Combine it with ArrayUtil and split() to normalize it before processing:

 

var selected = record.variables.multi_select.getDisplayValue().split(', ');
var cleaned = new ArrayUtil().unique(selected);

💡 Why This API Deserves the Spotlight

Most developers either don’t know this exists or forget it’s available. It’s not just syntactic sugar — ArrayUtil improves code readability, reduces boilerplate, and keeps your scripts aligned with platform best practices.


📝 Final Thoughts

Next time you find yourself writing a loop to check for values, or sanitizing arrays with custom logic — pause, and let ArrayUtil take the wheel. It might be unsung, but it’s certainly not unworthy.


🚀 Up Next in the Series
In the next edition of "The Unsung APIs of ServiceNow", I’ll be diving into the GlideDateTime API — another quiet powerhouse worth knowing.

 

If you know of any underused or overlooked ServiceNow APIs that deserve a spotlight, drop a comment—I’d love to explore them in future articles and credit you as a contributor.

 

Until then, happy building!
Sumanth Dosapati

Version history
Last update:
‎07-14-2025 03:17 AM
Updated by:
Contributors