From 395dc616846a85c4e2b3864e85e2b8b72bbfac81 Mon Sep 17 00:00:00 2001 From: keshava-palisetti Date: Tue, 28 Oct 2025 00:16:35 +0530 Subject: [PATCH 1/2] Create Readme.md Inbound Email Action to Create User and Assign Groups --- .../Readme.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/Readme.md diff --git a/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/Readme.md b/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/Readme.md new file mode 100644 index 0000000000..a3ac0440e3 --- /dev/null +++ b/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/Readme.md @@ -0,0 +1,25 @@ +Inbound Email Action to Create User and Assign Groups + +If an admin sends an email with specific user details, the script automatically: +Creates a new user (if not existing). +Assigns them to multiple groups. + +Create new Inbound Action: +Target table: sys_user +Type: New / Reply (depending on how you want it triggered) + +Example Email Format + +Subject: Create New User +Name: Abc Xyz +Email: abc.xyz@example.com +UserID: abc_xyz +Department: IT +Groups: Network Team, Application Support, Database Admins + +Working: + +Script reads each line from email body. +Extracts values for each field (Name, Email, etc.) using regex. +Checks if the user exists → if not, creates it. +Adds the user to the given list of groups. From 8696c87482b7ed52f906bb145a21d2b50a02f43f Mon Sep 17 00:00:00 2001 From: keshava-palisetti Date: Tue, 28 Oct 2025 00:17:38 +0530 Subject: [PATCH 2/2] Inbound email action code snippet Inbound Email Action to Create User and Assign Groups --- ...reate_user_through_inbound_email_action.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/auto_create_user_through_inbound_email_action.js diff --git a/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/auto_create_user_through_inbound_email_action.js b/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/auto_create_user_through_inbound_email_action.js new file mode 100644 index 0000000000..973081ef4e --- /dev/null +++ b/Server-Side Components/Inbound Actions/Inbound Email Action to Create User and Assign Groups/auto_create_user_through_inbound_email_action.js @@ -0,0 +1,69 @@ +(function runInboundAction(email, action) { + + var body = email.body_text; + + // Function to extract field values using regex + function fetchFieldValue(label) { + var regex = new RegExp(label + "\\s*:\\s*(.*)", "i"); + var match = body.match(regex); + return match ? match[1].trim() : ""; + } + + // Extract values from email + var name = fetchFieldValue("Name"); + var emailAddr = fetchFieldValue("Email"); + var userID = fetchFieldValue("UserID"); + var department = fetchFieldValue("Department"); + var groupsStr = fetchFieldValue("Groups"); + var groups = groupsStr ? groupsStr.split(",") : []; + + if (!emailAddr) { + gs.log("Inbound Email User Creation: Email missing. Aborting."); + return; + } + + // Check if user already exists + var existingUser = new GlideRecord("sys_user"); + existingUser.addQuery("email", emailAddr); + existingUser.query(); + + var userSysId; + + if (existingUser.next()) { + gs.log("User already exists: " + emailAddr); + userSysId = existingUser.sys_id.toString(); + } else { + // Create new user + var newUser = new GlideRecord("sys_user"); + newUser.initialize(); + newUser.name = name; + newUser.email = emailAddr; + newUser.user_name = userID; + newUser.department = department; + userSysId = newUser.insert(); + gs.log("New user created: " + name + " (" + emailAddr + ")"); + } + + // Add user to groups + groups.forEach(function(groupName) { + groupName = groupName.trim(); + if (groupName) { + var group = new GlideRecord("sys_user_group"); + group.addQuery("name", groupName); + group.query(); + if (group.next()) { + var mem = new GlideRecord("sys_user_grmember"); + mem.initialize(); + mem.user = userSysId; + mem.group = group.sys_id; + mem.insert(); + gs.log("User added to group: " + groupName); + } else { + gs.log("Group not found: " + groupName); + } + } + }); + +})(email, action); + +