Inbound action with data in sperate line

samadam
Kilo Sage

I have an inbound email that is coming with data in separate line, As is email actions are not creating and updating the fields. How can I parse this?

 

Type:

Test Type

Date:

02-03-2025

Any idea how to get this working?

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @samadam 

Here is the script you can try:

var body = "Type:\nTest Type\nDate:\n02-03-2025";
//var body = email.body_text;

function extractValue(label) {
    var pattern = new RegExp(label + ":\\s*([\\s\\S]*?)(\\n[A-Z][^:]*:|$)", "i");
    var match = pattern.exec(body);
    return match ? match[1].trim() : "";
}

var testType = extractValue("Type");
var testDate = extractValue("Date");

// Log values for debugging
gs.info("Email Body:\n" + body);
gs.info("Parsed Type: " + testType);
gs.info("Parsed Date: " + testDate);

Result:

JuhiPoddar_0-1746343095336.png

Note: Modify the script as needed for use in your Inbound Email Action.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

5 REPLIES 5

J Siva
Tera Sage

Hi @samadam 
Try the below script to format the email body before processing.

var str = `Key1:
Value1
key2:
Value2
Statement1
Statemet2`; // REPLACE THIS WITH THE EMAIL BODY
var result = str.replace(/:\r?\n/g, ":");
gs.print(result); 

Output:

JSiva_0-1746251759477.png

Hope this helps.
Regards,
Siva

 

Robert H
Mega Sage

Hello @samadam ,

 

Please put this function into your Inbound Email Action script:

 

function findValue(name) {
	var re = new RegExp('^' + name + ':[\r\n]+(.+)', 'm');
	var match = email.body_text.match(re);
	return match ? match [1] : '';
}

 

You can then use it like this:

gs.info('Type is: ' + findValue('Type'));
gs.info('Date is: ' + findValue('Date'));

 

Result:

Type is: Test Type
Date is: 02-03-2025

 

Regards,

Robert

Ankur Bawiskar
Tera Patron
Tera Patron

@samadam 

So what script did you start with and where are you stuck?

The email coming should have some standard format so that you can parse and get the content.

If the format changes every time then whatever script you write won't work and you will have to tweak the script every time

For your above format this script should help, please enhance it further if required

(function runInboundEmailAction(email, email_action, event) {
    // Get the email body
    var emailBody = email.body_text;

    // Initialize variables to store the extracted values
    var type = '';
    var date = '';

    // Split the email body into lines
    var lines = emailBody.split('\n');

    // Loop through each line to find and extract the Type and Date
    for (var i = 0; i < lines.length; i++) {
        var line = lines[i].trim();

        // Check for the Type line
        if (line.startsWith('Type:')) {
            type = lines[i + 1].trim(); // The next line contains the Type value
        }

        // Check for the Date line
        if (line.startsWith('Date:')) {
            date = lines[i + 1].trim(); // The next line contains the Date value
        }
    }

    // Log the extracted values (for debugging purposes)
    gs.info('Extracted Type: ' + type);
    gs.info('Extracted Date: ' + date);
   
})(email, email_action, event);

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

@samadam 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

Please mark appropriate responses as correct so that it benefits future readers.

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