The CreatorCon Call for Content is officially open! Get started here.

'Comments' in email notification

Riya17
Giga Contributor

Hi,

I have a requirement to show 'comments' in the request rejection email notification.  At the moment the script is not working.  It just shows blank space for 'Comments'.  Below is my notification, template and the script.   Could someone help me where it's going wrong.

Thank you 

 

Notification:

 

find_real_file.png

 

Template:

find_real_file.png

email script:

var comment = new GlideRecord("sysapproval_approver");
comment.addQuery("sysapproval", current.sys_id);
comment.query();
while (comment.next()) {
var comments = rec.comments.getJournalEntry(1);
template.print("<b>Comments:</b>" + comments + "\n");
template.print("<b>Comments:</b>" + comments.substring(comments.indexOf("\n")+1) + "\n");
}

 

 

15 REPLIES 15

Brad Tilton
ServiceNow Employee
ServiceNow Employee

It's because you don't have access to the current object from a background script.

Riya17
Giga Contributor

Hi Brad,

 

This is how it looks 😞

 

find_real_file.png

Brad Tilton
ServiceNow Employee
ServiceNow Employee

It's because you had the template print statement in twice, just simplify it like this:

var approval = new GlideRecord("sysapproval_approver");
approval.addQuery("sysapproval", current.sys_id).addOrCondition("document_id", current.sys_id);
approval.addQuery("state", "rejected");
approval.query();
while (approval.next()) {
    var comments = approval.comments.getJournalEntry(1);
    template.print(comments);
}

Riya17
Giga Contributor

Brad,

It looks fine except that the date and the name are still getting displayed.  Is there a way to hide?

 

Thanks

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You'd have to parse that out using regex. You might try something like the following:

https://community.servicenow.com/community?id=community_question&sys_id=8a274729db1cdbc01dcaf3231f96...

var comments = approval.comments.getJournalEntry(1);
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
template.print(comments.replace(dateRE, ''));