Inbound Email Parsing Body Text: Question?

Community Alums
Not applicable

Hey Everyone!

 

I have a quick question, I am not sure if this is possible, but I want to ask. 

 

I have an email body text formatted like this.

First name: test123

Last name: test12378

Business phone:

Best time to contact:

Preferred method: test123@gmail.com

 

Associated entity: Test Solutions, LLC

Category: Test

 

 

From the email body text, I want to get the first name and last name and map it to an external user field and the preferred method into the external user email field. Is this possible? 

 

Below is my inbound email action, action type is record action, and type is new. 

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
    var emailFrom = email.from;
    var emailSubject = email.subject;
    var name1 = email.body_text.first_name;
    var name = name1 + " " + email.body_text.last_name;
    // Implement email action here


    current.description = email.body_text;
    current.short_description = emailFrom + " " + email.subject;
    var sysUser = new GlideRecord("sys_user");
    sysUser.addEncodedQuery("email=" + email.body_text.preferred_method + "^active=true");
    sysUser.query();
    if (sysUser.next()) {
        current.internal_user = sysUser.getUniqueValue();
        current.u_external_contact = false;
    } else {
        current.u_external_contact = true;
        current.u_external_contact_name = name;
        current.u_external_contact_email = email.body_text.preferred_method;
    }
    current.insert();


})(current, event, email, logger, classifier);

 

This current code is half way working, the name and email comes in as undefined. 

3 REPLIES 3

Adrian Ubeda
Mega Sage
Mega Sage

Hello kmorgan10, 

I'll try to use regex to extract the exact data (using match function) that you are looking for instead using split or other methods. Try this as follows:

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
    var emailFrom = email.from;
    var emailSubject = email.subject;
    var emailBody = email.body_text;
    var name1 = emailBody.match(/\bFirst name:\s+\K\S+);
    var email = emailBody.match(/\bPreferred method:\s+\K\S+);
    var name = name1 + " " + email.body_text.last_name;
    //gs.info('name1: ' + name1 + "/ email:" + email);
    // Implement email action here


    current.description = email.body_text;
    current.short_description = emailFrom + " " + email.subject;
    var sysUser = new GlideRecord("sys_user");
    sysUser.addEncodedQuery("email=" + email + "^active=true");
    sysUser.query();
    if (sysUser.next()) {
        current.internal_user = sysUser.getUniqueValue();
        current.u_external_contact = false;
    } else {
        current.u_external_contact = true;
        current.u_external_contact_name = name;
        current.u_external_contact_email = email;
    }
    current.insert();


})(current, event, email, logger, classifier);

 Try to log the variables name and email to see if you are getting the exact info needed.

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

Community Alums
Not applicable

Hey Adrian! 

 

Thank you so much for this, but when I tried to use the code, it is throwing an error. 

Parsing error: Unterminated regular expression for the regex expressions. 

 

Thanks!

Hello kmorgan10, 
Try to declare this Regex before parsing and then apply match, I've tested on background script and worked:

var mailBody = email.body_text;
var regex = /First Name: (.+)/;
var name1 = mailBody.match(regex)[1];

 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆