Inbound Email Action: Parse Data From Table in Email to Create Multiple Incidents
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 06:29 AM
We have a project where ServiceNow will be receiving an inbound email, and needs to create Incidents off of it. The information needed to create the Incidents is found in an HTML table in the email. I have found other threads discussing similar issues, but this one appears to be a bit different than those ones I found.
In all the other ones I found, the field name was in the first column of the table, and the value was in the second column. My table is structured differently. It has field name in the first row of the table, then data runs under it. So it will look something like this:
So, there will always be at least one row of data, but there could be more.
From this, I need to create an Incident for each separate data row. So, from the example I above, I would need to create 3 separate Incidents, one for each data row.
How can I do this?
How can I parse my data table in the inbound email action, and create an Incident for each row (using the values/details from each row)?
So, my end result would be three separate Incidents. The Short Description of these Incidents would look like:
Incident 1: "Site Expiration Notice for abc.com"
Incident 2: "Site Expiration Notice for lmn.com"
Incident 3: "Site Expiration Notice for xyz.com"
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @jmiskey ,
Try to follow the below Steps .
- Navigate to System Policy > Email > Inbound Actions.
- Click New and set the following:
- Name: "Create Multiple Incidents from Table"
- Target Table: Incident [incident]
- Action Type: Record Action
- Type: New
- On the Conditions tab, define a subject line trigger.
Script is :
(function runAction(current, event, email, logger) {
var htmlContent = email.body_html;
var rowRegex = /<tr[^>]*>([\s\S]*?)<\/tr>/gi;
var cellRegex = /<td[^>]*>([\s\S]*?)<\/td>/gi;
var rows = [];
var match;
while ((match = rowRegex.exec(htmlContent)) !== null) {
rows.push(match[1]);
}
for (var i = 1; i < rows.length; i++) {
var cells = [];
var cellMatch;
while ((cellMatch = cellRegex.exec(rows[i])) !== null) {
cells.push(cellMatch[1].replace(/<[^>]*>/g, '').trim());
}
if (cells.length >= 2) { // Ensure row has minimum required data
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = cells[0]; // Assuming 1st column is Description
gr.description = "Created from inbound table row: " + cells[1];
gr.caller_id = gs.getUserID(); // Set caller to email sender
gr.insert();
}
}
event.state = "stop_processing";
})(current, event, email, logger);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am not sure what that last post was about. I am not looking for anything. This ticket was something I submitted back in 2024 that I said we moved off of and went ahead with a different solution. So this isn't anything we are actively working on.
chilakalasa just posted today asking how I did it (the original solution that we moved off of). So I found my old code and posted it for their benefit.
