- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 03:56 AM
I have one catalog item called "Request offboarding".
I need to create RITM through flow designer inbound email. So, I have created one flow as well, this is the trigger action:
After that I have create one action "Parse email body text".
This is the Parse email body text input:
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:
And variable function:
After that I set the look up flow :
Then I submit the catalog item and variable values as flow variable values:
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:01 AM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 05:21 AM
try to debug by keeping only this in your email to verify if it works
Body text:
Termination / Offboard
Employee: "Abel Tuter"
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:01 AM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:32 AM
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.
This is how I check my log in flow.
Can you please guide me what I am missing here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 10:40 PM
Did you check the email body was read correctly in that script?
try to print this in logs
inputs.email.toString()
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader