Extracting a Multi‐Line Description from an Email Body - ben-vargas/servicenow-wiki GitHub Wiki
When processing incoming emails in ServiceNow, you may need to parse the email body to extract specific information. For example, if the email includes a "description:" tag followed by multiple lines of text, you’ll want to capture all lines until the end of the email body and use that value in a record field, such as current.description
.
This article demonstrates how to use a regular expression (regex) and string manipulation to achieve this. The provided code snippet is intended for use in an Inbound Email Action, allowing you to programmatically extract a multi-line description from the email’s content.
Use Case
Scenario:
A user sends an email to a ServiceNow instance, intending to create or update a record with a detailed description. The email’s body contains a "description:" label, followed by multiple lines of text. You need to capture all lines after "description:" until the end of the email and store them in the current.description
field.
Goal:
Automatically parse and clean the multi-line description from the email body, ensuring that the resulting text is ready for insertion into a ServiceNow record.
Code Example
// Description: Extracts a multi-line description from the email body
// Maximum length limit or truncation can be handled separately if needed.
// Use a regex to find all text following "description:" until the end of the email body.
// The '[^]*' matches any character, including new lines, ensuring multiple lines are captured.
var re = /\ndescription:\s*([^]*)/;
var m;
// Attempt to match the regex against the email's body text.
// email.body_text is a standard variable available in Inbound Email Actions
if ((m = re.exec(email.body_text)) !== null) {
var descriptionClean = m[1];
// Replace encoded colons (%3A) with actual colons for cleaner output
descriptionClean = descriptionClean.replace(/%3A/g, ":");
// Assign the cleaned multi-line description to the current record's description field
current.description = descriptionClean;
}
Explanation of the Code
-
Regular Expression (Regex):
The regex pattern\ndescription:\s*([^]*)
looks for:\n
: A newline character before the word "description:".description:
: The literal string marking the start of the description section.\s*
: Any amount of whitespace after the colon.([^]*)
: A capturing group that matches any character (including newlines) until the end of the string.
By using
([^]*)
, all lines following "description:" are captured until no more text remains in the email body. -
Regex Execution (
re.exec
):
re.exec(email.body_text)
runs the regex against the email’s body text, which is provided by the Inbound Email Action context. If a match is found, it’s stored inm
. -
Cleaning the Result:
The captured groupm[1]
contains the multi-line description. The code replaces%3A
with:
to ensure any encoded colons are properly displayed. -
Assigning to the Record:
Finally,current.description
is set to the cleaned multi-line description. In an Inbound Email Action,current
refers to the record being created or updated based on the email.
How to Use
-
Inbound Email Action:
Go to System Policy > Email > Inbound Actions and create or edit an Inbound Email Action.- Condition: Adjust to trigger on specific subjects, senders, or email conditions.
- Action: Add the provided code snippet to the script field.
-
Testing:
Send a test email that includes a line starting with "description:" and multiple lines of text afterward. Once the email is processed, verify that the target record’s description field contains the expected multi-line text. -
Adjustments and Enhancements:
- Truncation or Length Limits:
If the description must not exceed a certain length, add a substring operation to limit the size before assigning it tocurrent.description
. - Different Tags or Patterns:
If you need to extract different tags (e.g.,\ntitle:
or\nnotes:
), adjust the regex accordingly. - Error Handling:
Add logs (gs.info
,gs.error
) or checks to handle cases where no match is found.
- Truncation or Length Limits:
Best Practices
- Regex Testing:
Test your regex pattern using a regex tester before implementing to ensure it captures the intended text. - Email Variations:
Consider variations in formatting (additional spaces, missing newline characters) and adjust the regex if necessary. - Documentation:
Comment your code and explain the pattern and reason for replacements, making future maintenance easier.
Conclusion
By using this approach, you can effectively extract a multi-line description from email body text into a ServiceNow field. This solution simplifies how inbound emails are processed, ensuring that users’ detailed input is captured accurately and made available in the record’s description.