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

Anurag Tripathi
Mega Patron
Mega Patron

I did a similar thing few days back



var messageContent = email.body_text;


if (messageContent.indexOf("From")!=-1)


                                      {


                                              messageContent = messageContent.substring(0, messageContent.indexOf("From")).trim();


                                      }


                                      else


                                      {


                                              messageContent = messageContent.trim();


                                      }



NOW messageContent   holds the most recent message body,



Hope this helps.


-Anurag

abrahams
Kilo Sage

Thank you so much for this idea.



Just one note - gmail ignores dsplay:none.   To get around this I used the following syntax:


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



I used both CSS syntax because Outlook didn't appear to like !important.   This line of code works for gmail.   The watermark will show in Outlook preview screen.   It doesn't show when you open up the email in Outlook.


bcronrath
Kilo Guru

Hello Brian,



I had a question about this item for the Business Rule:   "I've also added some conditions that the "target table" is only those tables where a reply inbound action has been configured."


I don't think I am quite understanding this portion of your solution, do you mean you added an "and" condition to the already existing "Type changes to send-ready" condition, that says "Target table [operator] [value]"?



For instance, if I only care about doing this for incident updates, would it just be "Target table is incident"?


Hi Brian, that's exactly what I mean.