Array of Objects
In ServiceNow, sometimes we need to fetch multiple records and store them in a structured format. The best way is to use an Array of Objects.
- Object: stores multiple field values of a single record.
- Array: stores multiple objects together.
This method is very useful when you want to return multiple records from a Script Include and use them in Background Scripts, UI Scripts, or Integrations.
Also you can return multiple user records.
Create an Array: var result = [];
Create an Object for each record: var obj = {field1: value, field2: value}
Push Object into Array: result.push(obj);
Return Array: from Script Include: return result;
Convert to JSON: in Background Script for readable output: JSON.stringify (data, null, 2).
Array of objects code

Code for run in Background Script

“We passed 3 user IDs, and the Script Include fetched details of those 3 users (email, last name, first name, username, location and department) and returned them as an array of objects.”