From 56294db919e61dae4b2ec093986ee4f9922e6116 Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Fri, 17 Oct 2025 23:59:56 +0530 Subject: [PATCH 1/2] Create readfile.md --- .../Extract Value from JSON/readfile.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Extract Value from JSON/readfile.md diff --git a/Server-Side Components/Background Scripts/Extract Value from JSON/readfile.md b/Server-Side Components/Background Scripts/Extract Value from JSON/readfile.md new file mode 100644 index 0000000000..13095e9a46 --- /dev/null +++ b/Server-Side Components/Background Scripts/Extract Value from JSON/readfile.md @@ -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 From 5796eecb9d6d2158af20966b258620e37120d5ee Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Sat, 18 Oct 2025 00:01:00 +0530 Subject: [PATCH 2/2] Create ExtractValuefromJSON.js --- .../ExtractValuefromJSON.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Extract Value from JSON/ExtractValuefromJSON.js diff --git a/Server-Side Components/Background Scripts/Extract Value from JSON/ExtractValuefromJSON.js b/Server-Side Components/Background Scripts/Extract Value from JSON/ExtractValuefromJSON.js new file mode 100644 index 0000000000..470559685c --- /dev/null +++ b/Server-Side Components/Background Scripts/Extract Value from JSON/ExtractValuefromJSON.js @@ -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); +})();