A script for extracting the body of an email (that contains no MSG-tag) into the Work notes of an incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2019 06:48 AM
Hi
I want to create an email inbound action that will extract the text in the body of the email into the Work notes on the Notes tab on the Incident form in ServiceNow. The e-mail that's incoming does not contain a MSG-tag, so the relevant Incident is recognized if the Incident number is present in the format "INCXXXXXXX" (X= any number between 0-9) anywhere in the Subject of the email.
Furthermore, I would like this extraction of the text to be executed only if the State of the Incident is either New, In Progress or On Hold.
I'm struggling with this a lot, and am relatively new to all this. So any help I would receive would be totally awesome. The code that I have so far - which I probably have messed up and which isn't working is this:
var str = email.subject;
var number = str.indexOf("INC");
var res = str.substring(number, number+7);
var getInc = new GlideRecord('incident');
getInc.comments = email.body;
if(getInc.query()){
getInc.comments = email.body;
getInc.update();
}
Kind regards
Marlowe Klingvall
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2019 10:18 AM
Hi,
Try this :
var str = email.subject;
var number = str.indexOf("INC");
var res = str.substring(number, number+10); //Number holds the value of I in INC, so to get the full INC number you need to add 10
var getInc = new GlideRecord('incident');
getInc.addQuery("number", res);
getInc.query(); // You would have to first build a query and run it to get to the exact Incident you are looking to update
if(getInc.next()){// Checks if the query returned a value
// Checks if the returned INC is one of these states : Please edit to match with your New, In Progress and On Hold values
if(getInc.state == "New" || getInc.state == "In Progress" || getInc.state == "On Hold"){
getInc.comments = email.body_text; // Returns the Email body in Text format
getInc.update();
}
}
Let me know if this works.
Please don't forget to mark the reply as Helpful/Correct if applicable. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2019 02:40 AM
Hi Allen A and Lokesh
Thank you so much for your replies. I have tried both your solutions and in all but one aspect they work out great. I can get the text in a body of an e-mail to be extracted and inserted into Activities in the Notes tab. The only thing is that it will not update the existing incident, the one that's referred to in the subject of the email, instead it creates a brand new incident. Now, I have tried to deactivate every other Inbound action, but that doesn't help any. So, I wonder, do you have any ideas about why this isn't working? Is there a Business rule or Client Script or the alike that I should deactivate/modify? I have looked through them but found nothing that I thought should interfer. What do you think?
Kind regards
/Marlowe Klingvall
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2019 07:32 AM
Hello again Allen and Lokesh
So I used both of your scripts and was able to get it to extract the text in the e-mail body, but for some reason that I don't know, it created a new incident. What I was able to find out though, is that ServiceNow cannot recognize and INC-number alone in either a Subject or in a Body of an e-mail that has no MSG, unless it has either the "RE:" or "RW:" at the beginning of the line. Once I figured that out, I was able actually update the incident in question with the text from the body in the e-mail, and thereby avoided the automatic creation of a new incident.
Whether this is good practice is definately debatable though.
Thanks for you help!
Kind regards
Marlowe Klingvall

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2019 07:39 AM
Hi,
Yea, the type would need to be set to "reply" for the inbound action and then in your email properties, you can see this:
Then perhaps you can get with your organization about coming up with a special "keyword" to use, and place that there, and then when they email in it will associate it to the incident. That was going to be the next thing I covered once you saw my post just a few minutes ago.
Now that this is solved, please mark my reply as Helpful/Correct, if it was.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!