Skip to content

Commit 5796eec

Browse files
Create ExtractValuefromJSON.js
1 parent 56294db commit 5796eec

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Script Name: Extract User Roles from JSON
3+
* Description:
4+
* This ServiceNow background script parses a JSON of user names and their titles (roles),
5+
* groups multiple titles under each unique user, and logs the result in System Logs.
6+
7+
* Requirements:
8+
* - Run this script in 'Scripts - Background' in ServiceNow.
9+
* - Avoids duplicate roles for the same user.
10+
* - Shows unique users and their corresponding roles.
11+
12+
* Author: Sachin Narayanasamy
13+
**/
14+
15+
(function() {
16+
// JSON containing usernames and titles
17+
var userData = {
18+
"userDetails": [
19+
{ "user_name": "dennis.millar", "title": "Account Exec Northeast" },
20+
{ "user_name": "ashley.parker", "title": "Director" },
21+
{ "user_name": "steve.schorr", "title": "Investigations Generalist" },
22+
{ "user_name": "tammie.schwartzwalde", "title": "Senior Auditor" },
23+
{ "user_name": "tammie.schwartzwalde", "title": "Payroll Generalist" },
24+
{ "user_name": "tommy.tom", "title": "Tester" }
25+
]
26+
};
27+
28+
// Object to hold unique users and their roles
29+
var userRoles = {};
30+
31+
// Iterate and group titles
32+
for (var i = 0; i < userData.userDetails.length; i++) {
33+
var user = userData.userDetails[i];
34+
var userName = user.user_name;
35+
var title = user.title ? user.title.trim() : "No Title";
36+
37+
if (!userRoles[userName]) {
38+
userRoles[userName] = [];
39+
}
40+
41+
if (userRoles[userName].indexOf(title) === -1) {
42+
userRoles[userName].push(title);
43+
}
44+
}
45+
46+
// Log results
47+
for (var userName in userRoles) {
48+
gs.info("User: " + userName + " having Role(s): " + userRoles[userName].join(", "));
49+
}
50+
51+
gs.info("Total unique users: " + Object.keys(userRoles).length);
52+
})();

0 commit comments

Comments
 (0)