logo

NJP

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

Import · Oct 02, 2019 · article

Hi All,

I have seen people post queries on retrieve values from HTML table from an email in an Inbound Action. I have tried to work on and get the desired output.

Usually we try to get the Value from plane text where we get the Name/Value pair as below:

:

Example:

Category: Software

Solution:

if (email.body.category != undefined)
{

current.category = email.body.category

}

What if you have a HTML table as below in your email , then the above method will not work as it should be. it would be real pain .

Incident Response Summary:
Priority: P1
Sensor: New
Protocol: TCP
Connected: Yes
Alert Volume: 167,194
Category: Software
Source Country: United States

Lets try to get the value of Category from above html table so please follow below steps:

Get the HTML body of the email

var htmlcode=email.body_html; 

Now remove all the HTML tags from the HTML body

htmlcode = htmlcode.replace(/<style([\s\S]*?)<\/style>/gi, '');
htmlcode = htmlcode.replace(/<script([\s\S]*?)<\/script>/gi, '');
htmlcode = htmlcode.replace(/<\/div>/ig, '\n');
htmlcode = htmlcode.replace(/<\/li>/ig, '\n');
htmlcode = htmlcode.replace(/<li>/ig, '  *  ');
htmlcode = htmlcode.replace(/<\/ul>/ig, '\n');
htmlcode = htmlcode.replace(/<\/p>/ig, '\n');
htmlcode = htmlcode.replace(/<br\s*[\/]?>/gi, "\n");
htmlcode = htmlcode.replace(/<[^>]+>/ig, '');
htmlcode=htmlcode.replace('  ','');

Now remove all the new line tags from the plain text

htmlcode=htmlcode.replace(/\r?\n|\r/g,'');

Now put the result in log:

gs.log("final email String  "+htmlcode);

you will get the email text as below:

CDTSensor:aresmgtlaeProtocol:TCPConnected:YesAlert Volume:167,194Category:SoftwareSource Country:United StatesHost Lookup:PSINet, Inc., United StatesDNS Lookup:COGENT-ASource Port:4091Destination IP:38.106.6.211Destination Country:United StatesHost Lookup:PSINet, Inc., United States

You will find something like below in this log:

Category:SoftwareSource Country

from this we need value of category as Software.

Now get the value of category from a htmlcode string using Regular expressions:

var indesREG = new SNC.Regex('/Category:.*Source Country/si');  // this line will get the value of string between Category: and Source Country

var finalvalue = indesREG.match(htmlcode).toString();

Here finalvalue will have our value of Category.

Now you can go ahead and update the value of category in an Incident Category field as below:

current.category=finalvalue;

That's all guys, Please mark it helpful , subscribe and give your valuable comments for improvement.

See you!

Regards,

Ajay

+91-9769949577

View original source

https://www.servicenow.com/community/itsm-articles/get-the-values-from-html-table-from-an-email-in-a-inblund-action/ta-p/2308675