Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Script Name: Extract User Roles from JSON
* Description:
* This ServiceNow background script parses a JSON of user names and their titles (roles),
* groups multiple titles under each unique user, and logs the result in System Logs.

* Requirements:
* - Run this script in 'Scripts - Background' in ServiceNow.
* - Avoids duplicate roles for the same user.
* - Shows unique users and their corresponding roles.

* Author: Sachin Narayanasamy
**/

(function() {
// JSON containing usernames and titles
var userData = {
"userDetails": [
{ "user_name": "dennis.millar", "title": "Account Exec Northeast" },
{ "user_name": "ashley.parker", "title": "Director" },
{ "user_name": "steve.schorr", "title": "Investigations Generalist" },
{ "user_name": "tammie.schwartzwalde", "title": "Senior Auditor" },
{ "user_name": "tammie.schwartzwalde", "title": "Payroll Generalist" },
{ "user_name": "tommy.tom", "title": "Tester" }
]
};

// Object to hold unique users and their roles
var userRoles = {};

// Iterate and group titles
for (var i = 0; i < userData.userDetails.length; i++) {
var user = userData.userDetails[i];
var userName = user.user_name;
var title = user.title ? user.title.trim() : "No Title";

if (!userRoles[userName]) {
userRoles[userName] = [];
}

if (userRoles[userName].indexOf(title) === -1) {
userRoles[userName].push(title);
}
}

// Log results
for (var userName in userRoles) {
gs.info("User: " + userName + " having Role(s): " + userRoles[userName].join(", "));
}

gs.info("Total unique users: " + Object.keys(userRoles).length);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Extract User Roles from JSON

---

## Overview
This ServiceNow background script takes a JSON of usernames and their titles (roles) and groups all roles under each **unique user**.
It prints the result in the **System Logs** in ServiceNow. This helps admins quickly see **unique users and all their roles**, avoiding duplicates.

---

## Use Case
For example, if a user appears multiple times with different roles:

- `tammie.schwartzwalde` appears as both:
- Senior Auditor
- Payroll Generalist

The script will show **all roles together** for that user, without repeating duplicates.

---

## Script Details

| Field | Value |
|-------|-------|
| Table | N/A (JSON based) |
| Type | Background Script |
| Author | Sachin Narayanasamy |
| Language | JavaScript (GlideRecord) |

---

## Logic Flow
1. Define the JSON array of user details.
2. Create an object to hold **unique users** and their roles.
3. Loop through each JSON entry:
- Trim and validate the title.
- Add it to the user's array if it doesn’t already exist.
4. Log each user and their unique roles.
5. Display the total number of unique users.

---

## Example Output
User: dennis.millar having Role(s): Account Exec Northeast
User: ashley.parker having Role(s): Director
User: steve.schorr having Role(s): Investigations Generalist
User: tammie.schwartzwalde having Role(s): Senior Auditor, Payroll Generalist
User: tommy.tom having Role(s): Tester
Total unique users: 5
Loading