Mail script to filter comments & work notes from "system"

michaelsharp
Mega Expert

We have the following mail script that we add to email templates that provide the last 3 comments and work notes.  I would like to be able to filter out the items posted by the "system" account.

We have inactivity workflows that will post work notes to the incident when it escalates.  After the 3rd escalation, the only comments & work notes you see are the system posting "escalated to level 1", "escalated to level 2", etc.  It would be helpful to actually see the latest comments and not just the escalation notes.

Not sure if this is possible since we're dealing with the sys_journal table, but thought I would ask to see if someone has done this before.

 

(function runMailScript(current, template, email, email_action, event) {

template.print('<p><font size="3" face="calibri">' + gs.getMessage('') + '</font></p>');

})(current, template, email, email_action, event);

4 REPLIES 4

robpickering
ServiceNow Employee
ServiceNow Employee

Michael,



I'd examine the business needs around putting "escalated to level 1" in your work notes (I'm assuming it's in work notes)...


We eliminated all automated comments and work notes from our environment because they honestly weren't providing any value.   If you're a fulfiller in the system you can see these escalations based on the re-assignment.   In later versions of ServiceNow you even get a timeline view of the assignment group changes and could have different notifications for escalations.   You can even provide different notifications on re-assignment if that's what you want to be communicated.



There's not a ton of value that those work notes or comments are making for the people reading them.   I'd just eliminate them.   If you don't believe me, ask your customers.   Chances are they'd prefer fewer notifications.



That's the Business View of the issue, from a technical answer perspective, you'd have to write a Script Include that would allow you to read the comments into an array and then filter out the ones you don't want.   The issue will be that you have no idea how many to go back, so you'll have to read them all in, and then filter, and then spit out the top 3.



-Rob


Thanks Rob.   The users, nor ITIL workers get email updates for this.   It's strictly a work note so you can see when the incident escalated and what level it escalated to.   We don't reassign on escalations, but we notify the tech, their manager and a larger group if the level gets high enough.   I agree the number of notifications needs to be kept to a minimum, hence why we do not trigger the alerts when the update is performed by "system".   Only human posts trigger notifications.



So, the question is still out there for someone that might know how to query that array, sort by date and then retrieve the last 3 results.   Not sure if it's possible, but it would be helpful to us.


Hi Michael,



Here is a mail script I use already that I've adapted to your purpose:



try{


var grEntry = new GlideRecord('sys_journal_field');


grEntry.addQuery('sys_created_by', '!=', 'system');


grEntry.addQuery('name', 'task');


var qc = grEntry.addQuery('element', 'comments');


qc.addOrCondition('element', 'work_notes');


grEntry.addQuery('element_id', current.sys_id);


grEntry.setLimit(3);


grEntry.orderByDesc('sys_created_on');


grEntry.query();


while(grEntry.next()){


template.print(grEntry.value.replace(/\[code\]|\[\/code\]/gi, ''));


}


}


catch(e){


gs.log("Error(" + e.lineNumber + "): " + e, "MAIL_SCRIPT");


}





The statements in bold are what I've added to accommodate your requirements and haven't been tested, but the original script worked without issue.   You could give that a try.



Thanks,


-Brian


Teena Singh
ServiceNow Employee
ServiceNow Employee

Michael,


The HI Customer Experience team is striving to ensure that customer queries posted from the HI Service Portal are answered in timely and accurate fashion.


If you feel your question has been resolved, please mark the appropriate reply in the thread as being the Correct Answer.


This enables other customers to learn from your thread.


Thank you in advance.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Regards,


Teena Singh
Customer Experience: UX Strategy and Customer Insights
teena.singh@servicenow.com


ServiceNow


Screen Shot 2017-01-19 at 8.55.52 AM.png