- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 02:10 PM
Version: Utah
Requirement A: When a comment on a Task-extended record is created, send an email to either user or assignment group (depends on who commented). The email contains the comment that was just inputted and only that comment. Possibility of multiple emails if multiple comments are rapidly entered, each email should contain the comment that triggered them specifically.
OR
Requirement B: Use the OOTB render of the comments field in emails and limit the # of entries to like...3. Simply just put ${comments} in the template. The verbiage will simply say "New comment/activity on ticket" and show the last 3 comments with time stamps.
Issue with A:
If I create a Notification with a trigger on "Comment changes", I can only access the comment via current.comments.getJournalField(1) but this only gets me the latest comment. The issue is what if the user or assignment group puts multiple comments a few seconds from each other? The users will be getting multiple emails with the last comment entered only.
Issue with B:
Ideally, I prefer this route but I want to tackle the off chance a user types up a long comment. I would like to limit the comments field to say....only 500 characters long. I tried following this KB article to limit the # of characters but I dont see it affecting either the body of the email nor if I go to the record.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 03:19 PM
Hi @Ace009 ,
To address the requirement and the issues you are facing you can follow the below approaches-
For Requirement A- to ensure that each comment triggers an email notification even when multiple comments are entered in quick succession, it's essential to handle the event correctly. Let's refine the code and add the necessary details for the notification. Create a update BR
(function executeRule(current, previous /*null when async*/) {
if (current.comments.changes()) {
var latestComment = getLatestComment(current);
gs.eventQueue('comment.added', current, latestComment, current.assigned_to);
}
function getLatestComment(current) {
var grJournal = new GlideRecord('sys_journal_field');
grJournal.addQuery('element_id', current.sys_id);
grJournal.addQuery('element', 'comments');
grJournal.orderByDesc('sys_created_on');
grJournal.setLimit(1);
grJournal.query();
if (grJournal.next()) {
return grJournal.value.toString();
}
return '';
}
})(current, previous);
For Requirement B- Create an Email Script to fetch and format the last three comments with character limits. please try the below script and modify it based on your requirements.
<g2:script>
(function() {
var commentsGr = new GlideRecord('sys_journal_field');
commentsGr.addQuery('element_id', current.sys_id);
commentsGr.addQuery('element', 'comments');
commentsGr.orderByDesc('sys_created_on');
commentsGr.setLimit(3);
commentsGr.query();
var formattedComments = '';
while (commentsGr.next()) {
var comment = commentsGr.value.toString();
if (comment.length > 500) {
comment = comment.substring(0, 497) + '...';
}
formattedComments += commentsGr.sys_created_by.getDisplayValue() + ' (' + commentsGr.sys_created_on.getDisplayValue() + '): ' + comment + '<br>';
}
gs.print(formattedComments);
})();
</g2:script>
use it in the message of the notification-
<g2:include src="email_script"/>
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 03:19 PM
Hi @Ace009 ,
To address the requirement and the issues you are facing you can follow the below approaches-
For Requirement A- to ensure that each comment triggers an email notification even when multiple comments are entered in quick succession, it's essential to handle the event correctly. Let's refine the code and add the necessary details for the notification. Create a update BR
(function executeRule(current, previous /*null when async*/) {
if (current.comments.changes()) {
var latestComment = getLatestComment(current);
gs.eventQueue('comment.added', current, latestComment, current.assigned_to);
}
function getLatestComment(current) {
var grJournal = new GlideRecord('sys_journal_field');
grJournal.addQuery('element_id', current.sys_id);
grJournal.addQuery('element', 'comments');
grJournal.orderByDesc('sys_created_on');
grJournal.setLimit(1);
grJournal.query();
if (grJournal.next()) {
return grJournal.value.toString();
}
return '';
}
})(current, previous);
For Requirement B- Create an Email Script to fetch and format the last three comments with character limits. please try the below script and modify it based on your requirements.
<g2:script>
(function() {
var commentsGr = new GlideRecord('sys_journal_field');
commentsGr.addQuery('element_id', current.sys_id);
commentsGr.addQuery('element', 'comments');
commentsGr.orderByDesc('sys_created_on');
commentsGr.setLimit(3);
commentsGr.query();
var formattedComments = '';
while (commentsGr.next()) {
var comment = commentsGr.value.toString();
if (comment.length > 500) {
comment = comment.substring(0, 497) + '...';
}
formattedComments += commentsGr.sys_created_by.getDisplayValue() + ' (' + commentsGr.sys_created_on.getDisplayValue() + '): ' + comment + '<br>';
}
gs.print(formattedComments);
})();
</g2:script>
use it in the message of the notification-
<g2:include src="email_script"/>
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar