We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Extracting data from the email body to add to short description on an incident

Pam Walker1
Tera Contributor

I have only been coding in ServiceNow for a short while so I would appreciate some guidance.

I am trying to get some data from an email body to add to the short description of the incident but I need to strip out the CR and LF.

 

The email body looks like this 

PamWalker1_0-1673884249667.png

I want to extract the User Name and Start date.

My code is 

var usname = email.body_text;

usname2 = usname.substring(usname.lastIndexOf("Name:"), usname.lastIndexOf("Division:"));
(usname2.trim());

usenddate = usname.substring(usname.lastIndexOf("Start Date:")+11, usname.lastIndexOf("Leave Date (if fixed term:)"));
(usenddate.trim());

current.short_description = email.subject + " - " + usname2 + "/" + usenddate;

 

But I am getting the CR and LF - please can someone help with the code

 

Thanks

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi ServiceNow supports name: value pairs for email scripting, and these should be available to you without any additional scripting required IE
IE var test = email.body.name

Setting field values from the email body (servicenow.com)

 

Otherwise lastIndexOf and indexOf support a start parameter, and '\n' is the new line character, so you can find the end of your line rather than looking for the begining of the next line\next 'name:'

JavaScript String indexOf() Method (w3schools.com)

 

Edit: you can test this in a background window

 

var email = {"body_text" :'This is a test\name: bob\nstuff \n Name: Use Me\n other stuff'};

gs.info(email.body_text);
var usname = email.body_text;
var indexStart = usname.lastIndexOf("Name:");
usname2 = usname.substring(indexStart + 5, usname.indexOf("\n", indexStart)).trim();
gs.info("myUser = " + usname2);

 

 

View solution in original post

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi ServiceNow supports name: value pairs for email scripting, and these should be available to you without any additional scripting required IE
IE var test = email.body.name

Setting field values from the email body (servicenow.com)

 

Otherwise lastIndexOf and indexOf support a start parameter, and '\n' is the new line character, so you can find the end of your line rather than looking for the begining of the next line\next 'name:'

JavaScript String indexOf() Method (w3schools.com)

 

Edit: you can test this in a background window

 

var email = {"body_text" :'This is a test\name: bob\nstuff \n Name: Use Me\n other stuff'};

gs.info(email.body_text);
var usname = email.body_text;
var indexStart = usname.lastIndexOf("Name:");
usname2 = usname.substring(indexStart + 5, usname.indexOf("\n", indexStart)).trim();
gs.info("myUser = " + usname2);