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

Hello Ankur!

 

I have a similar situation, in which we receive offboarding emails and I was following your suggestion to solve the other collegue situation. However, I'm struggling on how to create the action in the Flow Desginer. Could you help me on this?
Here's the input screen: 

barbaratosetto_0-1753483006590.png

 

Following your code, I created the script: 

barbaratosetto_1-1753483033132.png

barbaratosetto_2-1753483048451.png

 

I'm a bit confused regarding the Output variables. Should I create them in the script screen? or in the output step?

barbaratosetto_3-1753483087533.png


I created in both, just in case:

 

barbaratosetto_4-1753483111545.png

 

However, when I'm back to the parent flow, I cannot see the variables:

barbaratosetto_5-1753483165476.png

 

 

Any guidance how to build this action?

thank you in advance!