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

@PujaKar 

try to debug by keeping only this in your email to verify if it works

Body text:
Termination / Offboard

Employee: "Abel Tuter"
Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you for your help but somehow that is not working in flow action parse script.

I have attached my update set.

If possible can you please check this and guide me? that where is the gap?

 

 

Thank you,

Puja Kar

@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

I have tried your code in parse script.

(function execute(inputs, outputs) {

    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);
            }
        }*/

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

    } catch (ex) {
        gs.error(ex.message);
    }
    outputs.email_var = emailObj
})(inputs, outputs);

But still it is return {null} value in log but in BG it is fine.

PujaKar_0-1736432787230.png

This is how I check my log in flow.

PujaKar_1-1736433078348.png

Can you please guide me what I am missing here? 

 

@PujaKar 

Did you check the email body was read correctly in that script?

try to print this in logs

inputs.email.toString()

 

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