How to Parse Email Body text using Inbound Email in flow designer

Biso_Boby
Tera Contributor

I have a requirement to auto submit a catalog item, based on the inbound email content.  I have seen few links but seems they are all for plain text. 

 

I get the data in below table format, need to retrieve those field values and map it to catalog variables and submit the catalog item.

 

Contract ID4566
Contract NameTest
VendorTestXYZ

 

Can someone please assist.

 

@Ankur Bawiskar @Sandeep Rajput @Dr Atul G- LNG 

5 REPLIES 5

Nawal Singh
Mega Guru

Hi @Biso_Boby ,

you need to create a flow designer like shown in screenshot and set the field value using dot walk to inbound action-

NawalSingh_0-1759126835217.png

 and you can parse the body content like this-

 

NawalSingh_1-1759126932709.png

my requirement is to get the content from body that is inside in double quotes- for that i have written-

 

 var shortDescription="";
  var emailBody= fd_data.trigger.body_text;
  var regex = /"([^"]*)"/;
    var match = emailBody.match(regex);
    if (match) {
       shortDescription=  match[1];
    }
 return shortDescription;
 
 
If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Ankur Bawiskar
Tera Patron
Tera Patron

@Biso_Boby 

whatever you use inbound action or inbound flow, you will have to do string manipulation to get the content from HTML table

see these links and enhance your script as your table might be different.

Note: your script will only work when customer always replies in the same HTML table format

Get the values from HTML table from an email in a Inblund Action 

How to parse "Employee Name" from Email Body Text via a Inbound Action to Caller field? 

see this link for inbound flow parser for incoming email

Easiest way to Trigger Catalog Item Request via Inbound Email 

also check this

Launch a #ServiceNow Catalog Item from Email via Flow Designer (LIKE A BOSS) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Nawal Singh
Mega Guru

Hi@Biso_Boby ,

 

You can parse table content from body text like this-

 

var body = email.body_html;
var pattern = /<tr>(.*?)<\/tr>/g;
var rows = body.match(pattern);

for (var i = 1; i < rows.length; i++) { // skip header row
  var row = rows[i];
  var cells = row.match(/<td>(.*?)<\/td>/g);

  if (cells && cells.length >= 3) {
    var name = cells[0].replace(/<\/?td>/g, '').trim();
    var emailAddress = cells[1].replace(/<\/?td>/g, '').trim();
    var phone = cells[2].replace(/<\/?td>/g, '').trim();

    var user = new GlideRecord('sys_user');
    user.initialize();
    user.name = name;
    user.email = emailAddress;
    user.phone = phone;
    user.insert();
  }
}

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Biso_Boby To be frank, email parsing is not a reliable option. If a user changes the format or adds extra words here and there, the logic can easily break. However, since ServiceNow has changed this feature, you can try the links shared by other members in the thread.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************