- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 07:53 AM
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
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 12:23 PM - edited 01-16-2023 12:26 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 12:23 PM - edited 01-16-2023 12:26 PM
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);