- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2017 12:55 PM
Hi guys,
I'm still learning ServiceNow, so be gentle. I'm working on a workflow notification for Change Request that sends to the Requested By upon rejection and want to include the comments from the users that rejected it in the email notification.
I have a script I found in another old discussion thread that I've modified a bit to fit my needs, but my problem is it retrieves comments from any rejected approvals for the change request and not just from the current approval set. In other words, if it was rejected once and resubmitted, it's pulling in the comments from the previous rejection.
(function runMailScript(current, template, email, email_action, event) {
var app = new GlideRecord('sysapproval_approver');
var regex = /20.*\(Comments\)\n/g;
app.addQuery('sysapproval', current.sys_id);
app.addQuery('state', 'rejected');
app.query();
while (app.next()) {
template.print("Approver: " + app.approver.getDisplayValue() + ":\n");
template.print("Comments: " + app.comments.getJournalEntry(-1).replace(regex,"") + "\n\n");
}
})(current, template, email, email_action, event);
I think I've figured out I need to change the addQuery part, but I haven't been able to figure out from searching online what the proper query would be.
Thanks in advance for help!
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2017 12:56 PM
After some digging around through the GlideRecord query options I settled on adding a secondary query to filter the results down to just those from the most recent approval group:
(function runMailScript(current, template, email, email_action, event) {
var app = new GlideRecord('sysapproval_approver');
var regex = /20.*\(Comments\)\n/g; //strip out 'commments' line from each entry
// find all rejected approval records for the current Change ID, sorted by most recently created
app.addQuery('sysapproval', current.sys_id);
app.addQuery('state', 'rejected');
app.orderByDesc('sys_created_on');
app.query();
if (app.next()) {
// find all records within the current set that are from the most recent approval group
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('group', app.group);
gr.addQuery('state', 'rejected');
gr.query();
//for each result, return the approver's name and comments
while (gr.next()) {
template.print('<table><tr><td style="font-family: Arial, Helvetica, sans-serif;"><b>Approver:</b> ' + gr.approver.getDisplayValue() + '</td></tr>');
template.print('<tr><td style="font-family: Arial, Helvetica, sans-serif;"><b>Comments:</b> ' + gr.comments.getJournalEntry(1).replace(regex,"") + '</td></tr></table>');
}
}
})(current, template, email, email_action, event);
This is what the resulting email will look like:
Hope this is helpful to someone else!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2017 01:57 PM
This was very helpful! Thank you for sharing. It works great when rejecting a CR at a group level. However, we've added a second layer to the Change Request Approval Process which has a manager approval before the CR goes into a scheduled state.
When I reject at the group level, the notification shows exactly what I want it to by using your script with formatting I've added. The problem is at the manager level rejection. For whatever reason, when the CR is rejected at the manager level (after the group approves), there are several different CR rejection comments and approvers that appear in the email notification, rejection comments and approvers who are not even related to the current CR being approved. Something must be missing because they query does not care about the current Change ID.
I did not create another script or add on to yours. I simply tried to use it and format how I needed it to be. Like I said, it works great for approving at the group level, but for whatever reason, when a second level approval is added for the manager, there are a ton of CR rejection comments added to the notification, ones that are not even part of the current Change ID.
Any thoughts??
Thanks in advance!
-Marques
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2017 08:54 AM
Hmmm, without setting up a test workflow to be sure, I think it's because group approvals and individual approvals work slightly differently. Since we don't need to look up the group approval number to get the approvers, we should be able to skip my second query.
Here's a completely untested update to my script that I think should work. If it doesn't, let me know and I'll dig further.
(function runMailScript(current, template, email, email_action, event) {
var app = new GlideRecord('sysapproval_approver');
var regex = /20.*\(Comments\)\n/g; //strip out 'commments' line from each entry
// find all rejected approval records for the current Change ID, sorted by most recently created
app.addQuery('sysapproval', current.sys_id);
app.addQuery('state', 'rejected');
app.orderByDesc('sys_created_on');
app.query();
//for each result, return the approver's name and comments
while (app.next()) {
template.print('<table><tr><td style="font-family: Arial, Helvetica, sans-serif;"><b>Approver:</b> ' + app.approver.getDisplayValue() + '</td></tr>');
template.print('<tr><td style="font-family: Arial, Helvetica, sans-serif;"><b>Comments:</b> ' + app.comments.getJournalEntry(1).replace(regex,"") + '</td></tr></table>');
}
})(current, template, email, email_action, event);