- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 02:56 PM
Hi,
I have a little issue with parsing a certain value from an email. I am trying to parse the value "devicename" (R1 Choa Chu Kang) and then populating the cmdb_ci field on an incident form.
Below is a preview of the inbound email
Below is the script that processes the email
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
//Parse Table Content to Description
var parseBody = email.body_text.split(',').join('\n');
current.description = parseBody;
//Populate Email content to Fields
current.short_description = email.subject;
current.cmbd_ci.setDisplayValue(email.body.devicename);
gs.log("cmdb_ci = " + current.cmbd_ci.setDisplayValue);
current.insert();
})(current, event, email, logger, classifier);
And the logs show the value is "undefined"
Any help will be appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 06:49 PM
Hi, have you debugged to confirm that 'email.body.devicename' exists?
As the original data format is a comma separated string it would not be parsed as separate name: value pairs.
'The name:value pair must be on its own line. '
https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/administer/notification/reference/r_SetFieldValsFromTheEmailBody.html
The best solution would be to update the formatting of the source email so that separate lines are used for each name: value pair.
If this is not possible, then you can still parse the body and use javascript index and substring to return the required detail.
basic format
var messageBody = email.body_text;
var startIndex = messageBody.indexOf('devicename') + 10 // the size of this string;
var endIndex = messageBody.indexOf(',', startIndex); // find the next comma in the string
gs.info('startIndex ' + startIndex );
gs.info('endIndex ' + endIndex);
var myString = messageBody.substring(startIndex, endIndex);
//now remove any white space
myString = myString.trim();
gs.info(myString);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 06:49 PM
Hi, have you debugged to confirm that 'email.body.devicename' exists?
As the original data format is a comma separated string it would not be parsed as separate name: value pairs.
'The name:value pair must be on its own line. '
https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/administer/notification/reference/r_SetFieldValsFromTheEmailBody.html
The best solution would be to update the formatting of the source email so that separate lines are used for each name: value pair.
If this is not possible, then you can still parse the body and use javascript index and substring to return the required detail.
basic format
var messageBody = email.body_text;
var startIndex = messageBody.indexOf('devicename') + 10 // the size of this string;
var endIndex = messageBody.indexOf(',', startIndex); // find the next comma in the string
gs.info('startIndex ' + startIndex );
gs.info('endIndex ' + endIndex);
var myString = messageBody.substring(startIndex, endIndex);
//now remove any white space
myString = myString.trim();
gs.info(myString);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 06:47 AM
Extremely helpful answer. This also saved me a bunch of time as well. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 09:41 AM
This was amazingly helpful ... thank you .. save me a bunch-a-time!