Skip to content

Commit db76714

Browse files
authored
Select a random user from group (ServiceNowDevProgram#884)
* Create script.js * Create README.md * Update README.md
1 parent da58aaf commit db76714

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Random User Assignment
2+
3+
This function allows you to select a random user from a specified group. Use case could be on all task-like records that need to be worked on by someone and you want to select the person randomly.
4+
5+
6+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Get a random user from a specific user group
3+
*
4+
* @param {string} group - The sys_id of the group to select a user from
5+
* @returns {string|null} - The sys_id of a randomly selected user from the specified group or null if no user is found
6+
*/
7+
8+
function getRandomUserFromGroup(group) {
9+
10+
var users = [];
11+
12+
var grMember = new GlideRecord("sys_user_grmember");
13+
grMember.addNotNullQuery("user");
14+
grMember.addQuery("group", group);
15+
grMember.query();
16+
17+
while (grMember.next()) {
18+
users.push(grMember.getValue("user"));
19+
}
20+
21+
if (users.length > 0) {
22+
// Select a random user from the "users" array
23+
return users[Math.floor(Math.random() * users.length)];
24+
} else {
25+
// Return null if no user is found in the specified group
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)