How to parse email body text in flow designer inbound email?

PujaKar
Tera Contributor

I have one catalog item called "Request offboarding".

PujaKar_0-1736422569498.png

I need to create RITM through flow designer inbound email. So, I have created one flow as well, this is the trigger action:

PujaKar_1-1736422783633.png

After that I have create one action "Parse email body text".

PujaKar_2-1736422898348.png

This is the Parse email body text input:

PujaKar_3-1736422963569.png

and the script is: 

(function execute(inputs, outputs) {

/*var reg = inputs.bodyText.split(/\r\n|\r|\n/); // reg becomes an array.
var parsedObj = {};//split into 2d array
for(var i=0;i<reg.length;i++)
{  
reg[i] = reg[i].split(":");  
parsedObj[reg[i][0]]=reg[i][1];
}
gs.info("test log"+ JSON.stringify(parsedObj));*/

    function htmlToStr(html) {
        var noHTML = html.replace(/<br(?:\s*)?\/?>/gi, "\n").replace(/(<([^>]+)>)/ig, '');
        return decodeURI(noHTML);
    }

    

    try {
        // var body_text = inputs.email.replace(/<\/?[^>]+(>|$)/g, ""); //Remove HTML tags
        var body_text = htmlToStr(inputs.email.toString());
        var regEx = /^(?:\s+)?(.+?)(?:\s+)?:(?:\s+)?(.+)$/; //Name is everything preceding first :, value everything after.
        var matches;
        var emailObj = {};
        var bodyArr = body_text.split("\n");
            
        //loop through email body and build object
        for (var i = 0; i < bodyArr.length; i++) {
            try {
                matches = bodyArr[i].match(regEx);
                emailObj[decodeURI(matches[1].toString().trim())] = decodeURI(matches[2].toString().trim()); //Add trimmed name/val to object
            } catch (ex) {
                gs.error(ex.message);
            }
        }

    } catch (ex) {
        gs.error(ex.message);
    }
    //outputs.email_var = parsedObj; //return parsed name/val pairs
    outputs.email_var = emailObj
})(inputs, outputs);

Then I set the Flow variable:

PujaKar_4-1736423271330.png

And variable function:

PujaKar_5-1736423379109.png

After that I set the look up flow :

PujaKar_6-1736423445008.png

Then I submit the catalog item and variable values as flow variable values:

PujaKar_7-1736423487207.png

I use log as well but all the log return {null} value.

RITM is created but I am unable to set the RITM variable values through flow inbound, may be unable to parse the value from email body text.

My demo email is:

Subject: Workday: Offboard Employee - Abel Tuter(1457890)

Body text:
Termination / Offboard

Employee: "Abel Tuter"
EMP_ID: "1457890"
Effective_Date: "12/04/2024"
Position: P16634 PHLEBOTOMIST
Department: 107050 SBH Laboratory
Location: Bend St Charles
Email: "abel.tuter@example.com"

 

I need help on this to execute this requirement.

 

Thank you,

Puja Kar

1 ACCEPTED SOLUTION

@PujaKar 

I used your earlier code and it gave me only 1st value i.e. email

Then I updated your code as below and it gave me all the details. I used body[i].trim() while matching the regex to remove whitespaces

I believe from here onwards you can handle the complete requirement based on your developer skill set.

function htmlToStr(html) {
    var noHTML = html.replace(/<br(?:\s*)?\/?>/gi, "\n").replace(/(<([^>]+)>)/ig, '');
    return decodeURIComponent(noHTML);
}

try {
	var gr = new GlideRecord('incident');
	gr.get('9d385017c611228701d22104cc95c371');
	var email = gr.description;
    // Convert the email body from HTML to plain text
    var body_text = htmlToStr(email);
    var regEx = /^(?:\s+)?(.+?)(?:\s+)?:(?:\s+)?(.+)$/; // Name is everything preceding first :, value everything after.
    var matches;
    var emailObj = {};
    var bodyArr = body_text.split("\n");
    // Loop through email body and build object
    for (var i = 0; i < bodyArr.length; i++) {
        try {
            matches = bodyArr[i].trim().match(regEx);
            if (matches) {
                emailObj[matches[1].toString().trim()] = matches[2].toString().trim(); // Add trimmed name/val to object
            }
        } catch (ex) {
            gs.error(ex.message);
        }
    }
	gs.info(JSON.stringify(emailObj));

} catch (ex) {
    gs.error(ex.message);
}

Output:

inbound parse flow.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Hi @PujaKar ,

Please refer to this blog:

https://checksumfailed.com/tools/parseemail/

 

I visit this blog but I want the code to parse value. If possible can you please check the "parse email body text" code(which I upload in my query).

 

Ankur Bawiskar
Tera Patron
Tera Patron

@PujaKar 

seems it's not parsing correctly

Did you debug by adding logs?

I will suggest to try in background script first and then update the code in flow

Also try to leverage Transform Functions

Utilities transform functions 

Parsing values from email via Flow Desginer 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Yes! I check with logs in every stage but it is returning {null} value. If possible can you please check the "parse email body text" code(which I upload in my query).