- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 05:44 AM
I am trying to create multiple Incidents off a single email where the data needed is contained in a table in the email. I first tried to see if we could somehow parse the table, but due to the format of the table, it was not easy and I was unable to make any headway trying that method. So I am now approaching this from a different angle. I am using "email.body_text" to convert all the HTML code and tables to text, in hopes of being able to parse them to do what I need.
However, my experience with using Java to parse email text is fairly limited. I have always used "indexOf" to locate the line of data that I need, and used substrings to parse that particular line. But the data I need spans multiple lines. I am not sure if I need a loop, or something to that effect.
Here is the current structure of the email text after it has been converted to text. The format is always the same, the only difference is how many different blocks of site code we may have:
A bunch of text here...
We strongly recommend renewing the following custom certificates:
Site Name
Expiration
Days Left
Site 1
Expiration Date 1
Days Left 1
Site 2
Expiration Date 2
Days Left 2
This article...a bunch of text here
So basically, I need to get the data that resides between the words "Days Left" and "This article" (this is what is coming from the original data table). You can see that they reside in chunks of three lines for each entry (so there are two entries in the example above).
The data for one individual block may look like this:
2024-09-30
13
I need to create a separate Incident for each block of three (so in the example above, it should create two incidents). For each incident, I need to capture the Site Name and Expiration Date to put in the Description of each incident.
Can someone provide some guidance on how I can parse the data structured in this way to dynamically create the number of Incidents needed?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 05:59 AM
hi @jmiskey
Try bellow inbound email action once:
// Inbound Email Action script
(function() {
// Email body text
var emailBodyText = email.body_text;
// Extract relevant section of the email body
var startKeyword = "Days Left";
var endKeyword = "This article";
var extractedText = extractTextBetweenKeywords(emailBodyText, startKeyword, endKeyword);
// Parse the extracted text and create incidents
createIncidentsFromText(extractedText);
function extractTextBetweenKeywords(text, startKeyword, endKeyword) {
var startIndex = text.indexOf(startKeyword);
var endIndex = text.indexOf(endKeyword);
if (startIndex === -1 || endIndex === -1 || startIndex >= endIndex) {
gs.error("Invalid start or end keyword in the email body");
return "";
}
// Extract and return the text between the keywords
return text.substring(startIndex + startKeyword.length, endIndex).trim();
}
function createIncidentsFromText(text) {
var lines = text.split(/\r?\n/);
for (var i = 0; i < lines.length; i += 3) {
if (i + 2 < lines.length) {
var siteName = lines[i].trim();
var expirationDate = lines[i + 1].trim();
var daysLeft = lines[i + 2].trim(); // daysLeft is not used but can be logged or used as needed
// Create a new incident record
var incident = new GlideRecord('incident');
incident.initialize();
incident.short_description = 'Renewal needed for ' + siteName;
incident.description = 'Site Name: ' + siteName + '\nExpiration Date: ' + expirationDate;
incident.insert();
gs.info('Created incident for Site Name: ' + siteName + ' with Expiration Date: ' + expirationDate);
}
}
}
})();
I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 05:59 AM
hi @jmiskey
Try bellow inbound email action once:
// Inbound Email Action script
(function() {
// Email body text
var emailBodyText = email.body_text;
// Extract relevant section of the email body
var startKeyword = "Days Left";
var endKeyword = "This article";
var extractedText = extractTextBetweenKeywords(emailBodyText, startKeyword, endKeyword);
// Parse the extracted text and create incidents
createIncidentsFromText(extractedText);
function extractTextBetweenKeywords(text, startKeyword, endKeyword) {
var startIndex = text.indexOf(startKeyword);
var endIndex = text.indexOf(endKeyword);
if (startIndex === -1 || endIndex === -1 || startIndex >= endIndex) {
gs.error("Invalid start or end keyword in the email body");
return "";
}
// Extract and return the text between the keywords
return text.substring(startIndex + startKeyword.length, endIndex).trim();
}
function createIncidentsFromText(text) {
var lines = text.split(/\r?\n/);
for (var i = 0; i < lines.length; i += 3) {
if (i + 2 < lines.length) {
var siteName = lines[i].trim();
var expirationDate = lines[i + 1].trim();
var daysLeft = lines[i + 2].trim(); // daysLeft is not used but can be logged or used as needed
// Create a new incident record
var incident = new GlideRecord('incident');
incident.initialize();
incident.short_description = 'Renewal needed for ' + siteName;
incident.description = 'Site Name: ' + siteName + '\nExpiration Date: ' + expirationDate;
incident.insert();
gs.info('Created incident for Site Name: ' + siteName + ' with Expiration Date: ' + expirationDate);
}
}
}
})();
I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 06:16 AM
Wow!!! This does exactly what I need it to!
Thank you! Thank you! Thank you!