- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-25-2025 06:43 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-26-2025 08:34 AM - edited ā03-26-2025 08:35 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-26-2025 02:08 AM
Hi @AlfonsoP ,
Your approach is correct just make sure following points.
- You have created a before BR in incident table.
- In BR condition it should be State changes to Resolve.
- Invalid update will appear because of setAbortAction(true).
- Use below code.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if (!gs.hasRole('itil_admin'))
{
gs.addErrorMessage('You do not have the correct role to set the state to Resolved.');
current.setAbortAction(true);
}
})(current, previous);
I have implemented same in my PDI it works as expected.
If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers and me in getting recognized for my effort towards community.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-27-2025 01:56 AM
thx for your answer. I get your instructions but I received this messages:
I chose a non itil_admin user (bernard laboy)
How can we resolve this? š
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-27-2025 12:11 PM - edited ā03-27-2025 12:55 PM
Hi @AlfonsoP ,
Can you make sure that your BR is set to before ? I have implemented the same and itās working fine.
So here in your case BR is getting triggered but only itās not restricting incident to get resolve. Which possibly seems itās configured after or async.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-27-2025 01:56 AM
Hi @AlfonsoP ,
Did you check my solution to your question?
Please mark it helpful and accept the solution if it really helped you. This will help other community members and me as well.
Regards,
Rohit