change state restrict users

sai190
Tera Contributor

Hi can one helpme with the requirment 

 


CHANGE: Restrict Users from Closing Change Records

 

No one, including the admin role, should be able to close a change.  For Change States, "Closed Successful" and "Closed with Defects", only the Change Manager role should be allowed to set these states. 

7 REPLIES 7

Harneet Sital
Mega Sage
Mega Sage

Hi Sai, 

Create a before BR on the change request table as below - 

Conditions: State changes

Script: 

(function executeRule(current, previous /*null when async*/ ) {

    if (current.state == '3') // add the backend value for closed
    {
        gs.addErrorMessage('State cannot be changed to closed');
        current.setAbortAction(true);
    }

    if ((current.state == '2' || current.state == '5') && !gs.getUser().hasRole('change_manager')) // add the backend value for Closed Successful OR Closed with Defects
    {
        gs.addErrorMessage('State cannot be changed to closed successful or closed with defects unless you are a change manager');
        current.setAbortAction(true);
    }


})(current, previous);

 

Let me know if you need further help.


Thanks
-Harneet Sital (ServiceNow Certified Technical Architect)
Request you to please mark my answer as helpful or correct based on the impact

Find all my ServiceNow articles here

Hi even the admin sould not be able to access it

This code is not working for people who has admin, my ticket is only change manager can able to edit those two fields

Hi Sai, 

The last condition still will be true for admins as they have change_manager role inherited if you want to go beyond that then use you can use this instead and try -

(function executeRule(current, previous /*null when async*/ ) {

    if (current.state == '3') // add the backend value for closed
    {
        gs.addErrorMessage('State cannot be changed to closed');
        current.setAbortAction(true);
    }

    if ((current.state == '2' || current.state == '5') && (!gs.getUser().hasRole('change_manager') && gs.getUser().hasRole('admin'))) // add the backend value for Closed Successful OR Closed with Defects
    {
        gs.addErrorMessage('State cannot be changed to closed successful or closed with defects unless you are a change manager');
        current.setAbortAction(true);
    }


})(current, previous);

 

Ideally, this should help, but play around with conditions the way you expect them and the logic should hold true. 


Thanks
-Harneet Sital (ServiceNow Certified Technical Architect)
Request you to please mark my answer as helpful or correct based on the impact

Find all my ServiceNow articles here