Issue with Business Rule to Prevent Incident Resolution Without itil_admin Role

AlfonsoP
Tera Contributor

Hi everyone,

I'm trying to create a Business Rule in ServiceNow that prevents users without the itil_admin role from changing the state of an incident to "Resolved". I've written the following script, but I'm encountering the error message "Invalid update" and can't figure out what's wrong.

Here is the code I'm using:

 

(function executeRule(current, previous /*null when async*/) {
// Debug logs to check the current state and user roles
gs.log('Current state: ' + current.state);
gs.log('User roles: ' + gs.getUser().getRoles());

if (!gs.hasRole('itil_admin') && current.state.changesTo(4)) {
gs.addErrorMessage('You do not have the correct role to set the state to Resolved.');
current.setAbortAction(true); // Block the update action
}
})(current, previous);

 

My goal is to display an error message when a user without the itil_admin role attempts to set the incident state to "Resolved" and block the update action. However, the user is still able to update the incident to "Resolved", and the requested error message appears along with another message saying "Invalid update".

Does anyone have suggestions on what might be causing the "Invalid update" error or how to improve this script?

Thanks in advance for your help

1 ACCEPTED SOLUTION

Nishant8
Giga Sage

Hello @AlfonsoP, can you please change your if condition as follows and then try?

if (!gs.hasRole('itil_admin') && current.state.changesTo(6)) {
gs.addErrorMessage('You do not have the correct role to set the state to Resolved.');
current.setAbortAction(true); // Block the update action
}

 a few points to consider:

- it should be before business rule

- invalid update message is outcome of setAbortAction(true); its alright if you get this message on screen

- since OOB Resolve UI action marks State filed to Resolved as soon as its pressed, user's will notice the value of State filed as Resolved on the screen even though Incident is failed to update successfully. users will have to refresh the page manually to see original/older value

- The page can't be refreshed automatically, if required, using BR now as setRedirect doesn't work with setAbortAction šŸ˜ž

- If its really require to refresh then you may have to write Client Script additionally

 

I hope the above will help you write the script...

 

Regards,

Nishant

View solution in original post

12 REPLIES 12

Hi @AlfonsoP ,

 

I believe I have also provided the correct solution and first one to provide the solution. You can mark more than one solution as accepted solution. This will help me for my efforts towards the community. 

 

Regards,

Rohit 

Nishant8
Giga Sage

Hello @AlfonsoP, can you please change your if condition as follows and then try?

if (!gs.hasRole('itil_admin') && current.state.changesTo(6)) {
gs.addErrorMessage('You do not have the correct role to set the state to Resolved.');
current.setAbortAction(true); // Block the update action
}

 a few points to consider:

- it should be before business rule

- invalid update message is outcome of setAbortAction(true); its alright if you get this message on screen

- since OOB Resolve UI action marks State filed to Resolved as soon as its pressed, user's will notice the value of State filed as Resolved on the screen even though Incident is failed to update successfully. users will have to refresh the page manually to see original/older value

- The page can't be refreshed automatically, if required, using BR now as setRedirect doesn't work with setAbortAction šŸ˜ž

- If its really require to refresh then you may have to write Client Script additionally

 

I hope the above will help you write the script...

 

Regards,

Nishant

oh, so it is normal to see the following messages:

You do not have the correct role to set the state to Resolved.
Error Message
Invalid update
Info Message
Incident INC0010016 has been resolved

and then refreshing the page, it will be not resoved.
There is some change to put to not see the info message "Info Message
Incident INC0010016 has been resolved"?

Ankur Bawiskar
Tera Patron
Tera Patron

@AlfonsoP 

that's the OOB behavior whenever you use setAbortAction(), system shows "Invalid update" error.

You cannot hide that

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

so it is normal to see the info message 

Info Message

Incident INC0010016 has been resolved

" even if we have set the business rule with abort action?