Get Comment when Rejecting from approvals list view - ajcooper72/ServiceNow GitHub Wiki

This article explains the UI action for getting rejection comments when rejecting from a list view (update_set_ui_action_reject_with_reason.xml)

Overview

The implementation consists of three parts;

  1. UI Action - Reject (with Comment)
  2. Ajax Script include - ApprovalRejectCommentsAjax
  3. UI Page - approval_rejection_reason

UI Action

This simply provides the client script for handling the reject request. It works for both context menu and the choice menu at the bottom, meaning it can handle single or multiple rejections in one go (at the moment it applies the same comment to all selected approvals). Having to do some fudging with g_list and the ID's as we can't access g_list in the method called from the GlideModal.

var rejectCommentsDialog;
var glist;
var checkIds;

function loadRejectComments(current) {

	if (g_list.getChecked() != '') {
		checkIds = g_list.getChecked();
	} else {
		if (typeof rowSysId == 'undefined') {
			checkIds = gel('sys_uniqueValue').value;	
		} else {
			checkIds = rowSysId;	
		}
	}

	glist = g_list;
	rejectCommentsDialog = new GlideModal("approval_rejection_reason", false, 648, 250);
	rejectCommentsDialog.setTitle(new GwtMessage().getMessage("Reject Approval Request"));
	rejectCommentsDialog.render();
}

function rejectApproval(rejectComment) {

	var ga = new GlideAjax("ApprovalRejectCommentsAjax");
	ga.addParam("sysparm_name", "setRejectReason");
	ga.addParam("sysparm_tu_reason", rejectComment);
	ga.addParam("sysparm_tu_approval", checkIds);
	ga.getXMLAnswer(function() {
		rejectCommentsDialog.destroy();
		glist.refresh();
	});
}

if (typeof window == 'undefined')
	setRedirect();

function setRedirect() {
	current.update();
	action.setRedirectURL(current);
}

Ajax Script Include

The next part is the Ajax script include that is called by the client script. This simply works through each of the selected records, adds the comment and flags as rejected.

var ApprovalRejectCommentsAjax = Class.create();
ApprovalRejectCommentsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	setRejectReason: function() {
		
		var sysId = this.getParameter("sysparm_tu_approval");
		var reason = this.getParameter("sysparm_tu_reason");

		var sysIds = sysId.split(',');
		for (var i = 0; i < sysIds.length; i++) {
			var grApproval = new GlideRecord('sysapproval_approver');
			if (grApproval.get(sysIds[i])) {
				grApproval.state = 'rejected';
				grApproval.comments = gs.getMessage('Rejected reason: ') + reason;
				grApproval.update();
				new ApprovalUserFeedback().rejected(grApproval);
			}	
		}

		return true;
	},

	type: 'ApprovalRejectCommentsAjax'
});

UI Page

Finally, the UI page simply grabs the message from the user.

HTML

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:dialog_notes_ok_cancel
		dialog_id="approval_reject_reason"
		textarea_id="approval_reject_reason_text"
		textarea_label="${gs.getMessage('Reason')}"
		textarea_label_title="${gs.getMessage('A reason is required to reject this approval')}"
		textarea_name="approval_reject_reason_text"
		textarea_onkeyup="enableButton()"
		textarea_onchange="enableButton()"
		textarea_style="height:auto; width: 100%; resize: vertical;"
		textarea_title="${gs.getMessage('Enter the reason here')}"
		ok=""
		ok_action="rejectApprovalButton"
		ok_id="change_confirm_ok_btn"
		ok_title="${gs.getMessage('Reject Approval')}"
		ok_type="button"
		ok_style_class="btn btn-primary disabled"
		cancel_title="${gs.getMessage('Close the dialog')}"
	/>
</j:jelly>

Client Script

function rejectApprovalButton() {
	var textArea = $("approval_reject_reason_text");
	if (textArea)
		rejectApproval(textArea.value.trim());
}

(function() {
	$("approval_reject_reason_text").focus();
})();
⚠️ **GitHub.com Fallback** ⚠️