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

matt_staples
Mega Contributor

One thing to note is that this code only works for top replies (i.e. mail clients that put the reply at the top). This is fine for 99% of email clients, but for users who do bottom replies (like some people in our third level teams...) it runs the risk of stripping out replies.



Preventing this only requires a small modification though.



Business Rule:


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


    if (mark != '') {  


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


          var str2 = current.body;


          var str3 = mark;


          var result = str1.concat(str2) + str3 + '\n\n';  


          current.body = result;  


      }  


You could also just use 'var result = str1.concat(str2) + str1 + '\n\n'; However having a separate str3 like this leaves the bottom watermark visible on the email reply, which ensure that users enter their text after it.



Inbound rule:


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 watermarks  


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


      }  


    current.comments = "reply from: " + email.origemail + "\n\n" + body;


Mike McCall
Giga Guru

This should also be possible using default functionality in the system: Email Properties - Inbound Mail Configuration (glide.pop3.reply_separators)


(Or, for those doing a better job keeping up with the new versions, hah: Inbound mail configuration)


ceulert
Giga Contributor

Since Quebec, you have the Email Reply Separator table and the sys_properties glide.pop3.reply_separators is deprecated.

https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/administer/notification/task/pars...