- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2014 02:05 PM
Hello! I am attempting to edit the 'change.itil.approver.reject' template to include the name of the approver and the reject comments in the notification to the requester.
I created this mail script:
<mail_script>
var chRej = new GlideRecord("sysapproval_approver");
chRej.addQuery("sysapproval", current.sys_id);
chRej.query();
while (chRej.next()) {
email.setSubject("Your Change Request has been rejected by" + chRej.approver);
template.print("<b>Comments:</b>" + chRej.comments + "\n");
}
</mail_script>
The notification fires when the approval is rejected by email and via the interface, however the variables are not appearing in the email. The comments are blank, and the 'chRej.approver' variable shows up as what looks like a GUID of some sort (i.e f37a9a636f1c210087f8d4e44b3ee4e8). So, I end up with this:
---
Subject: Your Change Request has been rejected byf37a9a636f1c210087f8d4e44b3ee4e8
Body:
Comments:
Ref:MSG0005012
---
Am i not querying the correct table? Any assistance here would be appreciated!
Cheers!
Solved! Go to Solution.
- Labels:
-
Service Mapping

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2014 09:58 PM
Hi Tim,
Building on what Donnie said, you can use the getJournalEntry() method to retrieve all comments or specific comments. If you retrieve the latest comment using getJournalEntry(1), the comment will appear with a header (showing a date/time stamp and the user who added the comment, followed by the multi-line comment. If you wish to drop the header and only show the comment, you can use the substring() and indexOf() methods to truncate the comment string and start it from the second line. See below for an example.
<mail_script>
var chRej = new GlideRecord("sysapproval_approver");
chRej.addQuery("sysapproval", current.sys_id);
chRej.query();
while (chRej.next()) {
email.setSubject("Your Change Request has been rejected by" + chRej.approver.getDisplayValue());
var comments = rec.comments.getJournalEntry(1);
template.print("<b>Comments:</b>" + comments + "\n"); // shows full comment entry with header}
template.print("<b>Comments:</b>" + comments.substring(comments.indexOf("\n")+1) + "\n"); // shows comment only with no header
</mail_script>
Regards,
Jake
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2014 12:51 PM
You have the correct table. For the approver I would try either chRej.approver.name or chRej.approver.getDisplayValue(). Either should work.
For the comments field, try chRej.comments.getJournalEntry(1).
The issue with the comments field is that it's a journal field - in this case I think a journal_input field (one of three types of journal fields). These are stored differently than other fields and have to be access differently. The getJournalEntry() function, as its name suggests, can be used to return journal entries. The integer passed determines which entry to return. The only values I have ever had to use are -1 (which returns all entries, separated by '\n\n') and 1, which returns the most recent. For your purposes, the most recent is probably all you need.
See: Using Journal Fields

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2014 09:58 PM
Hi Tim,
Building on what Donnie said, you can use the getJournalEntry() method to retrieve all comments or specific comments. If you retrieve the latest comment using getJournalEntry(1), the comment will appear with a header (showing a date/time stamp and the user who added the comment, followed by the multi-line comment. If you wish to drop the header and only show the comment, you can use the substring() and indexOf() methods to truncate the comment string and start it from the second line. See below for an example.
<mail_script>
var chRej = new GlideRecord("sysapproval_approver");
chRej.addQuery("sysapproval", current.sys_id);
chRej.query();
while (chRej.next()) {
email.setSubject("Your Change Request has been rejected by" + chRej.approver.getDisplayValue());
var comments = rec.comments.getJournalEntry(1);
template.print("<b>Comments:</b>" + comments + "\n"); // shows full comment entry with header}
template.print("<b>Comments:</b>" + comments.substring(comments.indexOf("\n")+1) + "\n"); // shows comment only with no header
</mail_script>
Regards,
Jake

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2014 08:38 AM
Hi Jake,
One question here. Is it possible to enter "Rejection comments" mandatory at the time of rejecting approvals.
Regards,
Harish.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2014 04:35 PM
Hi Harish,
Yes you can make the comments field mandatory for rejecting approvals. However, you may need to change the way the form works "out of the box". The UI Actions (buttons) for Approve and Reject and "server side" which means they update the "current" object and then perform a record update. Forcing comments to be mandatory for "Reject" only means you need to know that (1) the "Reject" button has been clicked or (2) the state is changing to "Rejected". I would suggest creating a replacement UI Action using the same "action name" as the original ("reject"), but making it a "client" version. This code will run when the Reject button is clicked, so you can check if the comments field is empty and inform the user that the field is mandatory. See the script below as an example:
function checkComments(){
var comments = g_form.getValue('comments');
if(comments==''){
alert('Please enter a comment');
}
else{
g_form.setValue('state','rejected');
g_form.save();
}
}
Ensure you use the same "Condition" values and the same "Action name" as the original UI Action (e.g reject), so that it replaces the original button. Then add "checkComments()" to the "Onclick" field.
Also, see the ServiceNow Wiki for further information:
Regards,
Jake