Parsing Text for Inbound Email Action

jmiskey
Kilo Sage

This is probably more of a Java Script text parsing question than a ServiceNow specific question, but I am trying to use this in an Inbound Email Action.

 

We have emails that come into us whose Subject line follow this format:

FW: Security Notification - Name - EmployeeID - Effective Date - Date - ACTION REQUIRED

 

The values in red are the variable amounts, while the rest is hard-coded text.

We need to pull out the Employee ID and Date values from this string.

I was initially thinking we could split the string by the dash, but some people may have dashes in their name.

Another complicating factor is that the EmployeeID can be either 6 or 7 characters long.

 

Here is one example of what the actual data strings may look like (note the text is really not colored, I just use colors below to highlight what we are trying to pull out of it):

FW: Security Notification – John Doe - 0123456 - Effective Date - 2025 05 24 - ACTION REQUIRED

So in the above example, we would want to pull out the "0123456" and the "2025 05 24".

 

Another example may look like this:

FW: Security Notification – Jane Doe-White - 123456 - Effective Date - 2025 05 31 - ACTION REQUIRED

So it this example, we want want to pull out the "123456" and the "2025 05 31".

 

Can anyone who is an expert at parsing data like this in Java Script show me the most efficient way to do this that will work in both examples (where the number of dashes and characters in the EmployeeID may differ)?

 

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

something like this

function parseEmailSubject(subject) {
    // Regular expression to match the pattern
    var regex = /FW: Security Notification - (.+?) - (\d{6,7}) - Effective Date - (\d{4} \d{2} \d{2}) - ACTION REQUIRED/;
    var match = subject.match(regex);
    
    if (match) {
        var name = match[1];
        var employeeID = match[2];
        var date = match[3];
        
        return {
            name: name,
            employeeID: employeeID,
            date: date
        };
    } else {
        return null;
    }
}

// Example usage
var subject1 = "FW: Security Notification - John Doe - 0123456 - Effective Date - 2025 05 24 - ACTION REQUIRED";
var subject2 = "FW: Security Notification - Jane Doe-White - 123456 - Effective Date - 2025 05 31 - ACTION REQUIRED";

var result1 = parseEmailSubject(subject1);
var result2 = parseEmailSubject(subject2);

gs.info(result1.name); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.name); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

gs.info(result1.employeeID); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.employeeID); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

gs.info(result1.date); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.date); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

Output:

AnkurBawiskar_0-1745583242483.png

 

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

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

something like this

function parseEmailSubject(subject) {
    // Regular expression to match the pattern
    var regex = /FW: Security Notification - (.+?) - (\d{6,7}) - Effective Date - (\d{4} \d{2} \d{2}) - ACTION REQUIRED/;
    var match = subject.match(regex);
    
    if (match) {
        var name = match[1];
        var employeeID = match[2];
        var date = match[3];
        
        return {
            name: name,
            employeeID: employeeID,
            date: date
        };
    } else {
        return null;
    }
}

// Example usage
var subject1 = "FW: Security Notification - John Doe - 0123456 - Effective Date - 2025 05 24 - ACTION REQUIRED";
var subject2 = "FW: Security Notification - Jane Doe-White - 123456 - Effective Date - 2025 05 31 - ACTION REQUIRED";

var result1 = parseEmailSubject(subject1);
var result2 = parseEmailSubject(subject2);

gs.info(result1.name); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.name); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

gs.info(result1.employeeID); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.employeeID); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

gs.info(result1.date); // { name: 'John Doe', employeeID: '0123456', date: '2025 05 24' }
gs.info(result2.date); // { name: 'Jane Doe-White', employeeID: '123456', date: '2025 05 31' }

Output:

AnkurBawiskar_0-1745583242483.png

 

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

Thank you!  That works perfectly!

Does exactly what I need it to!