Inbound email action to store user list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 02:55 AM
Hi Team,
Requirement is - customer send an email with an .csv attachment with user list (they will send only user ID in the attachment), once we received the email, we need to read that attachment and store the user list in the company's (already we have a company name "xyz") user's.
how to write inbound email action for the above scenario, Write the code but not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 10:37 PM
Hello @GD00609570
Try below:
var excp = ""; // Store exceptions
gs.log("Enter email processing");
// Check email subject
if (email.subject.indexOf("PingCo NOC User List for Visibility Group Automation") !== -1) {
gs.log("Matching email subject found.");
var userList = [];
// Query the sys_attachment table directly using the email's sys_id
var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery("table_name", "sys_email");
attachmentGR.addQuery("table_sys_id", email.sys_id);
attachmentGR.addQuery("content_type", "text/csv"); // Ensure it's a CSV
attachmentGR.orderByDesc("sys_created_on");
attachmentGR.query();
if (!attachmentGR.next()) {
gs.log("No CSV attachment found in email.");
return;
}
// Read the CSV attachment
var gsa = new GlideSysAttachment();
var attachmentData = gsa.getContent(attachmentGR);
var csvContent = String(attachmentData);
// Parse CSV content
var lines = csvContent.split("\n");
for (var i = 0; i < lines.length; i++) {
var userID = lines[i].trim();
if (userID) {
userList.push(userID);
}
}
gs.log("Extracted user IDs: " + JSON.stringify(userList));
if (userList.length === 0) {
gs.log("No valid user IDs found in CSV.");
return;
}
// Store users in sys_user table under company 'xyz'
var companySysId = getCompanySysId("xyz");
for (var j = 0; j < userList.length; j++) {
var userId = userList[j];
// Check if user already exists
var userGR = new GlideRecord("sys_user");
userGR.addQuery("user_name", userId);
userGR.query();
if (userGR.next()) {
gs.log("User already exists: " + userId);
} else {
// Create new user
var newUser = new GlideRecord("sys_user");
newUser.initialize();
newUser.user_name = userId;
newUser.company = companySysId;
newUser.insert();
gs.log("Created new user: " + userId);
}
}
}
/**
* function to get the sys_id of a company by name
*/
function getCompanySysId(companyName) {
var companyGR = new GlideRecord("core_company");
companyGR.addQuery("name", companyName);
companyGR.query();
if (companyGR.next()) {
return companyGR.getUniqueValue();
}
return "";
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 10:40 PM
try this
var excp = ""; // Store exception
gs.log("Enter email");
// Check if the email subject matches
if (email.subject.indexOf("PingCo NOC User List for Visibility Group Automation") !== -1) {
var emailBody = email.body_text;
gs.log("Read email subject");
// Exception handling for wrong body text
var firstLine = /PingCo Users for Visibility Groups attached/;
var firstLineisMatching = emailBody.match(firstLine);
if (firstLineisMatching) {
// Read the data from CSV attachment
var userList = [];
var emailGR = new GlideRecord("sys_email");
emailGR.addQuery("subject", "LIKE", "%PingCo NOC User List for Visibility Group Automation%");
emailGR.orderByDesc("sys_created_on");
emailGR.setLimit(1);
emailGR.query();
if (emailGR.next()) {
var emailSysId = emailGR.getUniqueValue();
var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery("table_name", "sys_email");
attachmentGR.addQuery("table_sys_id", emailSysId);
attachmentGR.orderByDesc("sys_created_on");
attachmentGR.addQuery("content_type", "text/csv");
attachmentGR.query();
if (attachmentGR.next()) {
var gsa = new GlideSysAttachment();
var attachmentData = gsa.getBytes(attachmentGR);
var attachment = String(Packages.java.lang.String(attachmentData));
var lines = attachment.split('\n');
for (var i = 0; i < lines.length; i++) {
var userId = lines[i].trim();
if (userId) {
userList.push(userId);
}
}
// Store users in the company's user list
for (var j = 0; j < userList.length; j++) {
var userGR = new GlideRecord("sys_user");
userGR.addQuery("user_name", userList[j]);
userGR.query();
if (userGR.next()) {
userGR.company.setDisplayValue("xyz");
userGR.update();
}
}
} else {
excp += "There are no CSV attachments for this email record \n";
}
}
} else {
excp += "Incorrect email body format \n";
}
if (excp.length > 0) {
gs.info("Exception: " + excp);
}
}
Also check this
Loading data from an email attachment
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader