Truncate email replies to include only newest content

Brian Arndt1
Mega Expert

When someone replies to a ServiceNow-generated email, most inbound actions are set up to add the reply as a comment or work note to the ticket (e.g., incident) that includes the entire email body of the reply. This quickly grows the size of the email/comments when conversations go back and forth several times, often making the newest content difficult to find. Not having seen any other solutions for this problem, I developed my own using three simple ServiceNow objects.

  • Custom system property - to establish a watermark to be used to determine where the new content ends
  • Business rule - to write the watermark into the email
  • Inbound action - to interpret the reply and truncate the content using the watermark

The watermark is a string that represents a unique pattern of characters that should not normally be found in an email response. The use of the system property allows you to both "activate" the watermarking and establish the string used as the watermark. I've chosen the string //--*--// as the watermark.

The business rule is a before insert/update rule on the sys_email table with the condition that "type" changes to "send-ready". I've also added some conditions that the "target table" is only those tables where a reply inbound action has been configured. When it runs, it retrieves the watermark from the system property and inserts it into a hidden div element at the beginning of the email body.

var mark = gs.getProperty('gaig.email.watermark.truncate_reply');

if (mark != '') {

  var str1 = '<div style="display: none;">'+ mark +'</div>';

  var str2 = current.body;

  var result = str1.concat(str2);

  current.body = result;

}

The inbound action then finds the watermark in the reply and removes everything after it, only posting the content before the watermark into the new comment/work note.

var body = email.body_text.toString();

var mark = gs.getProperty('gaig.email.watermark.truncate_reply'); //get the value of the watermark

if (mark != '') {

        var truncBody = body.split(mark); //if there's a watermark, split the string at the watermark

        body = truncBody[0].toString(); //keep everything before the watermark

}

current.comments = 'received from: ' + mail + '\n\n' + body;

The result is a significantly less-cluttered stream of information added to the ticket history.

Screen Shot 2015-04-13 at 11.17.56 AM.png

13 REPLIES 13

Am i doing something wrong with the Inbound rule here. The system property and business rule work fine (watermark is inserted into emails), but the Inbound rule doesn't seem to take effect.



find_real_file.png


Never mind, i found the issue. Our 'mail' variable is different. Should of noticed sooner.


Ah, yeah, that would do it too


Hi Matt,



The code provided in the example is just an excerpt. You still need to update the record with current.update().


This might also help you - works perfectly for stripping email replies and showing only the latest reply in the ticket body (incident in the example)


Managing Email Replies | Glass 'putan with Service-Now