How to concatenate old mail content and latest email reply received

GarimaU
Tera Expert

Use case: There is a requirement, where on receiving an email, an interaction should be created. The mail body text should be displayed on 'description' field.  And if we get reply to the mail, the description should be update.

I have created 2 inbound action, one for New type, other for Reply type. 

Ques: How to showcase mail that was send for creating an interaction and on the same field, if any reply comes, that should also be displayed. Basically concatenate both emails.

1 ACCEPTED SOLUTION

Hi Ankur, On Reply inbound email action I wrote below script and It worked for interaction table.

current.u_description =
"\n\n============================\n" +
"Reply from " + email.from + " on " + new GlideDateTime().getDisplayValue() + "\n" +
"----------------------------\n" +
email.body_text +
"\n============================\n" +
(current.u_description || '');

View solution in original post

4 REPLIES 4

Abbas_5
Tera Sage
Tera Sage

Hello @GarimaU,

 

To concatenate old mail content with the latest email reply in ServiceNow, you can use a combination of Inbound Email Actions and scriptingThe core idea is to process the incoming email reply and append it to the existing conversation, potentially filtering out repeated text. Here's a breakdown of the steps:
 
1. Configure Inbound Email Actions:
  • Identify the Target Table:
    Determine the table (e.g., Incident, Task) where the email conversation is stored.
  • Create an Inbound Email Action:
    This action will be triggered when an email is received that matches certain criteria (e.g., replying to a specific email or a pattern in the subject line).
  • Define Action Steps:
    • Update the relevant field(s): Typically, you'd update the comments field (for example) or a dedicated field for email history.
    • Process the email body: You'll need to extract the relevant part of the email reply (the new content) from the email's body. This may involve parsing the email using methods like string manipulation or regular expressions to remove watermarks or previous email content. 
       
2. Scripting for Email Content Extraction and Concatenation:
  • Use Script Include or Client Script: Create a script include or client script to perform the string manipulation and concatenation.
  • Access the relevant fields: Get access to the current record's email history and the email's body from the incoming email. 
     
  • Extract the new email content: Parse the email body to extract the user's reply, potentially removing any watermarks or previous email content. 
     
  • Concatenate the content: Combine the new email content with the existing email history.
  • Update the target field: Update the target field with the concatenated content. 
     
Example (Conceptual):
 
// Inside a Script Include or Client Scriptfunction concatenateEmailContent() {  var emailBody = email.body_text; // Get email's body  var existingComments = current.comments; // Get existing comments  // -- Extract the new email content from emailBody (using regex or string manipulation) --  var newContent = extractNewContent(emailBody); // Example function to extract new content  // -- Concatenate the new content with the existing comments --  current.comments = existingComments + "\n\n" + newContent; // Simple example  // -- Update the Comments field --  current.update();}//Example function to extract New content:function extractNewContent(emailBody){  // Remove watermark if present.  Watermark example: "---  Original Message Below ---"  var watermark = "---  Original Message Below ---";  if (emailBody.indexOf(watermark) > -1){    var index = emailBody.indexOf(watermark);    emailBody = emailBody.substring(0, index);    // Remove any lines after the watermark.    emailBody = emailBody.trim();  }  return emailBody;}// -- Call the function within the Inbound Email Action (or a Business Rule) --concatenateEmailContent();
Key Considerations:
  • Watermarks:
    ServiceNow automatically inserts watermarks (e.g., "--- Original Message Below ---") in outbound emails to aid in identifying replies and forwards. You'll need to handle these watermarks when extracting the new content. 
     
  • Email Format:
    Consider both plain text and HTML emails when extracting content. You might need to handle HTML formatting differently than plain text. 
     
  • Performance:
    Be mindful of the performance implications of querying the sys_email table, especially if you're fetching a large number of emails. Consider using other methods like the journal to store email history if necessary. 
     
  • User Experience:
    Design the user interface to display the email history in a clear and organized manner. 
     
  • Advanced Email Parsing:
    For more complex scenarios, you might consider using regular expressions or specialized libraries for email parsing and content extraction. 
     
By carefully configuring Inbound Email Actions and using appropriate scripting techniques, you can effectively concatenate old mail content with the latest email reply in ServiceNow, creating a clear and informative email conversation history.
 
If it is helpful, please hit the thumbs up icon and accept the correct solution by referring to this solution in future it will be helpful to them.
 
Thanks & Regards,
Abbas Shaik

Ankur Bawiskar
Tera Patron
Tera Patron

@GarimaU 

you need to have a journal field OR concatenate the description field

in your Reply type inbound email action you can concatenate it just before the update

current.description = current.description + email.body_text;

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

@GarimaU 

Hope you are doing good.

Did my reply answer your question?

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

Hi Ankur, On Reply inbound email action I wrote below script and It worked for interaction table.

current.u_description =
"\n\n============================\n" +
"Reply from " + email.from + " on " + new GlideDateTime().getDisplayValue() + "\n" +
"----------------------------\n" +
email.body_text +
"\n============================\n" +
(current.u_description || '');