to restrict Change raiser to approve own change request

hydu95358
Tera Contributor

Hi

 

I need to restrict the change raiser from approving own changes

Please suugest

2 REPLIES 2

hydu95358
Tera Contributor

Wrote below code in the sysapproval table but still if the raiser and the approver are different still it is showing the error message and changing the state to cancelled

 

var gr = new GlideRecord('change_request');
gr.setWorkflow(false);
gr.addQuery('sys_id', current.sysapproval);
gr.query();
if(gr.next()) {
var req = gr.requested_by.toString();
var app = current.approver.toString();
if (req == app) {
gs.info("I am inside the change1"+req+app);
gr.setWorkflow(false);
current.setValue('state', 'cancelled');
gs.addErrorMessage('Cannot approve, as the approver ['+current.approver.getDisplayValue() + '] is the Change Raiser.');
// gs.addErrorMessage('cannot approve due to approver ['+current.approver.getDisplayValue() + ']being the same as requester [' + gr.getDisplayValue('requested_by') + ']');
}

Karthiga S
Kilo Sage

HI @hydu95358 

 

To restrict a change raiser from approving their own change request in ServiceNow, 

1. Navigate to System Definition > Business Rules.
2. Click on New to create a new business rule.
3. Give the business rule a name, such as "Prevent Self Approval".
4. In the 'When to run' section, select 'before' and 'insert, update'.
5. In the 'Table' field, select 'Change Request [change_request]'.
6. In the 'Advanced' tab, write a script to check if the approver is the same as the change raiser. Here is a sample script:

(function executeRule(current, previous /*null when async*/) {
if (current.requested_by == current.approver) {
gs.addErrorMessage('You cannot approve your own change request.');
current.setAbortAction(true);
}
})(current, previous);


7. Click on Submit to save the business rule

 

This script will prevent the change request from being saved if the approver is the same as the person who requested the change. The 'gs.addErrorMessage' function displays a message to the user, and 'current.setAbortAction(true)' cancels the save operation.

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga