Parsing email body from an Inbound Email action

Waseem Abbas
Kilo Contributor

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

find_real_file.png

 

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"

 

find_real_file.png

Any help will be appreciated. 

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

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);

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron

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);

Extremely helpful answer. This also saved me a bunch of time as well. Thank you!

Jason Siegrist
Giga Guru

This was amazingly helpful ... thank you .. save me a bunch-a-time!