Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Parsing email body into Inbound Email Action

StewartFletcher
Tera Guru

Good day,

 

I'm trying to figure out how to get data out of a table in an email, and I've scoured multiple articles and utilised this already - How to read body text in Inbound EMAIL Flow - ServiceNow Community

However, it doesn't work for our instnace because the emails come in a tabled format, with no colons or anything defining the end of a word. Example -:

Title

 

First name

Test

Last name

McTest

 

When I tested the parsing utility in our sandbox, I was sending emails in and putting a - between Title and whatever the title is, and it would find it. However, we cannot re-format these emails (there's no option for us to do this without significant development time from another team) so I'm having to try and find a solution to this.

Could anybody suggest how we can achieve this please? I've seen various articles with scripts etc, but none of them work for us.

1 ACCEPTED SOLUTION

StewartFletcher
Tera Guru

I've managed to work through and resolve this one myself. I had to amend the parsing script quite a lot, and in the end feed it a list of items in an array to be able to identify which should be the keys, and including blank values. Here's the script in case this helps anybody in future - the third replace in the htmlToStr function are table headers that we had that I needed to strip out. Further down this also strips the 

 

(function execute(inputs, outputs) {

    function htmlToStr(html) {
        return html.replace(/<br(?:\s*)?\/?>/gi, "\n")
            .replace(/(<([^>]+)>)/ig, '')
            .replace(/(?:Details 1|Details 2|Details 3)\s*\n?/gi, '');
    }

    try {
        var expectedFields = [
            "Title", "First name", "Last name", "Address line 1", "Address line 2",
            "Address line 3", "Town / city / region", "Postcode:", "Email address",
            "Telephone number"
        ];

        var body_text = htmlToStr(inputs.email.toString());
        var bodyArr = body_text.split("\n").map(function(line) {
            return line.trim();
        }).filter(function(line) {
            return line.length > 0;
        });

        var emailObj = {};
        for (var i = 0; i < bodyArr.length; i++) {
            var key = bodyArr[i];
            if (expectedFields.includes(key)) {
                var value = (i + 1 < bodyArr.length && !expectedFields.includes(bodyArr[i + 1])) ? bodyArr[i + 1] : '';
                emailObj[key] = value;
                if (value !== '') i++; // Skip value line
            }
        }
       
        if (emailObj["Email address"]) {
        emailObj["Email address"] = emailObj["Email address"]
        .replace(/<.*?>/g, '') // Remove anything in angle brackets
        .replace(/mailto:/gi, '') // Remove 'mailto:' if present
        .trim();
}


        outputs.email_var = emailObj;

    } catch (ex) {
        gs.error("Email parsing error: " + ex.message);
        outputs.email_var = {};
    }

})(inputs, outputs);

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@StewartFletcher 

this article should help you, remember your script will only work if the incoming email always follow that HTML table format.

If they deviate then your logic will break

Get the values from HTML table from an email in a Inblund Action 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It doesn't as that example table has : after each item, ours doesn't

I even tried manually amending the email to put : after the item and it didn't work. It works if I type out Title -  outside of the table, but not in the table.

@StewartFletcher 

I believe you will require string manipulation in order to cater to your requirement.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Any assistance you can provide with that at all please?