to restrict Change raiser to approve own change request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 04:58 AM
Hi
I need to restrict the change raiser from approving own changes
Please suugest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 06:31 AM
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') + ']');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 07:36 AM
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