Skip to content

Commit 59ddb1e

Browse files
Inbound Email Action to Create User and Assign Groups (#2550)
* Create Readme.md Inbound Email Action to Create User and Assign Groups * Inbound email action code snippet Inbound Email Action to Create User and Assign Groups
1 parent 97b5e29 commit 59ddb1e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Inbound Email Action to Create User and Assign Groups
2+
3+
If an admin sends an email with specific user details, the script automatically:
4+
Creates a new user (if not existing).
5+
Assigns them to multiple groups.
6+
7+
Create new Inbound Action:
8+
Target table: sys_user
9+
Type: New / Reply (depending on how you want it triggered)
10+
11+
Example Email Format
12+
13+
Subject: Create New User
14+
Name: Abc Xyz
15+
Email: abc.xyz@example.com
16+
UserID: abc_xyz
17+
Department: IT
18+
Groups: Network Team, Application Support, Database Admins
19+
20+
Working:
21+
22+
Script reads each line from email body.
23+
Extracts values for each field (Name, Email, etc.) using regex.
24+
Checks if the user exists → if not, creates it.
25+
Adds the user to the given list of groups.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
(function runInboundAction(email, action) {
2+
3+
var body = email.body_text;
4+
5+
// Function to extract field values using regex
6+
function fetchFieldValue(label) {
7+
var regex = new RegExp(label + "\\s*:\\s*(.*)", "i");
8+
var match = body.match(regex);
9+
return match ? match[1].trim() : "";
10+
}
11+
12+
// Extract values from email
13+
var name = fetchFieldValue("Name");
14+
var emailAddr = fetchFieldValue("Email");
15+
var userID = fetchFieldValue("UserID");
16+
var department = fetchFieldValue("Department");
17+
var groupsStr = fetchFieldValue("Groups");
18+
var groups = groupsStr ? groupsStr.split(",") : [];
19+
20+
if (!emailAddr) {
21+
gs.log("Inbound Email User Creation: Email missing. Aborting.");
22+
return;
23+
}
24+
25+
// Check if user already exists
26+
var existingUser = new GlideRecord("sys_user");
27+
existingUser.addQuery("email", emailAddr);
28+
existingUser.query();
29+
30+
var userSysId;
31+
32+
if (existingUser.next()) {
33+
gs.log("User already exists: " + emailAddr);
34+
userSysId = existingUser.sys_id.toString();
35+
} else {
36+
// Create new user
37+
var newUser = new GlideRecord("sys_user");
38+
newUser.initialize();
39+
newUser.name = name;
40+
newUser.email = emailAddr;
41+
newUser.user_name = userID;
42+
newUser.department = department;
43+
userSysId = newUser.insert();
44+
gs.log("New user created: " + name + " (" + emailAddr + ")");
45+
}
46+
47+
// Add user to groups
48+
groups.forEach(function(groupName) {
49+
groupName = groupName.trim();
50+
if (groupName) {
51+
var group = new GlideRecord("sys_user_group");
52+
group.addQuery("name", groupName);
53+
group.query();
54+
if (group.next()) {
55+
var mem = new GlideRecord("sys_user_grmember");
56+
mem.initialize();
57+
mem.user = userSysId;
58+
mem.group = group.sys_id;
59+
mem.insert();
60+
gs.log("User added to group: " + groupName);
61+
} else {
62+
gs.log("Group not found: " + groupName);
63+
}
64+
}
65+
});
66+
67+
})(email, action);
68+
69+

0 commit comments

Comments
 (0)