Extract text from inbound email and post to description field on target record

John Prahn
Kilo Contributor

Hi All, 

I have a requirement to extract data from an inbound email and post to the short description field on the target record. 

We have an integration setup using notifications and when the record is initiated on the other system, it will respond in email with that ticket number. 

I have attached a sample of the inbound email. I am looking to extract the line: Ticket #xxxxxxxx. 

 

Thanks in advance!

30 REPLIES 30

cmcclendon
Mega Guru

First step is to create an inbound email action using the table, like Incident, as your target table. Next step is the tricky part. You'll need to write a script in the inbound action the searches the email.body_text field for your ticket #. You will probably need to do a standard  string.search() form here. Make sure you set your When to Run up very carefully to do this on certain emails coming from a specific sender with a standard subject line, otherwise you will slow inbound email down searching thru a ton of inbound email. Let me know if you need help with the Javascript side for string extraction.

Good luck!

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Tough one, if your ticket number were on "Ticket: INC0012345", you could just use "email.body.something". Though, because you have a line "Ticket" and line "Ticket:" is something really different in your case, you would need to parse the whole body (email.body_text).

If "Ticket:" then this would already work:

(function() {
	
	var ticket = email.body.ticket;
	
	var grIncident = new GlideRecord('incident');
	grIncident.newRecord();
	grIncident.setValue('short_description', ticket);
	grIncident.insert();
		
})();

Is there a possibility to alter the incoming mail at the source?

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

This works at my personal dev instance, just tested with an email looking like your example.

(function() {

	var str = email.body_text;
	
	var ticket = str.substring(str.lastIndexOf("#"), str.lastIndexOf("Ticket:"));
	
	var grIncident = new GlideRecord('incident');
	grIncident.newRecord();
	grIncident.setValue('short_description', ticket);
	grIncident.insert();
		
})();

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Update the code a little bit to remove the new line:

var ticket = str.substring(str.lastIndexOf("#"), str.lastIndexOf("Ticket:")).trim();

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn