Need to inbound email action script for ticket creation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 03:00 AM
hello everyone,
we need help on inbound email script,
we have requirement that , whenever we received a mail from backup team, we need to look into their emails from last 2 hours , if we found the same attributes values repeats in their email body from last three emails, we need to create incident for the third email, attributes look like this , PFB screenshot, i have written the script to look in to email table but it's not working , anyone please help me that would be great help,
(note - Any one attribute value repeats in three emails also we can consider)
example, in the below screenshot , if location values repeats three times in the last three email, we need to consider to create incident
#inbound #email #script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 03:29 AM
Hi @siva14 you verify with following updated Script:
(function processInboundEmail() {
var senderEmail = email.from;
var emailBody = email.body_text.trim();
var emailCountGR = new GlideRecord('sys_email');
emailCountGR.addQuery('user_id', senderEmail);
emailCountGR.addOrCondition('user', senderEmail);
emailCountGR.addEncodedQuery('sys_created_on>=javascript:gs.beginningOfLast2Hours()'); // Filter by last 2 hours
emailCountGR.addQuery('type', 'received');
emailCountGR.addQuery('receive_type', 'new');
emailCountGR.orderByDesc('sys_created_on');
emailCountGR.setLimit(3);
emailCountGR.query();
var locations = [];
while (emailCountGR.next()) {
var body = emailCountGR.body_text.toString();
var locationPattern = /Location\s*:\s*(\S[\s\S]*?)(?=\n|$)/i;
var match = body.match(locationPattern); // Match the pattern in the email body
if (match && match[1]) {
locations.push(match[1].trim());
}
}
if (locations.length === 3 && locations[0] === locations[1] && locations[1] === locations[2]) {
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
incidentGR.short_description = "Location Repeated in 3 Emails: " + locations[0];
incidentGR.description = "The location: '" + locations[0] + "' has been repeated 3 times in the last 3 emails from the backup team.";
incidentGR.insert();
}
})(email);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 03:43 AM
Why not do this through an inbound email flow? Trigger it on an email coming in from the backup team and then do a lookup to the other emails from the team of the last 2 hours.
Set the location from the incoming email as flow variable (and the same with any other value) and loop through the other emails to find similar emails (you can add a 'count' variable to update the counter). If the counter reaches 3, you create an incident from the last email (you can even do a check to see if the incident already exist when the 4th or 5th email comes in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 04:24 AM
Hi @Mark Manders , we can try , but i am struck with how to get the location value , and how to check with Other emails, if you help that would be great help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 04:39 AM
I was struggling with this a while back myself, until I found the perfect solution for this. Check out Robert Fedoruk's video where he explains Ben Scherer's solution for this, which he shared on the share: https://developer.servicenow.com/connect.do#!/share/contents/8408077_parse_email_flow_action?v=2.4&t...
Parsing the values from email is a huge improvement and will work perfectly for getting the values you are after.