When To Use setAbortAction()
A common use case in ServiceNow for an Admin or Developer is to not allow a record to be inserted or updated, under certain conditions.
This is where the method setAbortAction() becomes very useful.
The setAbortAction(), usually run as current.setAbortAction(true) stops the current record in the server side script from being updated in ServiceNow. So think business rule or script include. There are ways to abort an action on the client side, but we can’t use this method there. There are always going to be scenarios that you want to prohibit in your ServiceNow environment. You don’t want users being able to update all fields at all times. Sometimes an ACL is more appropriate or eve a simple UI Policy.
The feature that you will build out is always going to be in response to the specific use case presented by the business. There is not a one size fits all answer to many “How do I …” questions on the ServiceNow Platform.
For example, say that you want to require a Work Note to be added to an Incident, whenever the Assignment Group changes. Maybe there’s a manager that is requesting that whenever you change assignment groups, you need to state why you. are doing this.
Like most features in ServiceNow, this can be accomplished in a few different ways. But here, we’ll utilize a Business Rule and abort the action, if attempted. We will even add an informational message on the form, to assist the user in taking the correct action. Take the script and modify based on your requirements. This is really meant as a basic introduction to setAbortAction and to show people how it can be utilized in the platform.
Using setAbortAction()
Role Required: admin
It’s important to note that you also should be running this operation, BEFORE the record is inserted/updated. If you take a moment to think about why, this will make more sense. There is an order of operations around the execution of business rules in ServiceNow. You can run rules right before, or right after the record is inserted/updated. It makes sense to abort the record BEFORE it’s inserted/updated – so the record doesn’t already exist in the table.
Go ahead and create a new Business Rule.
You can modify the name, but the other information needs to be set up like the image below.
Once you’ve done that, submit/save the business rule and move over to the Advanced tab to start building your script.
For the condition, you can either use the Condition builder, or you can script it out. We will use the scripting ability for this.
CONDITION:
current.assignment_group.changes() && current.work_notes.nil()
SCRIPT:
(function executeRule(current, previous /*null when async*/ ) {
current.setAbortAction(true);
gs.addInfoMessage(“Add a Work Note when changing the Assignment Group”);
})(current, previous);
This ends up being a rather straight forward business rule.
All the condition says is: run this script IF:
– The assignment group field changes
AND
– The work notes are empty
You can even get more creative with your condition here. You could limit it to a certain group, users with a certain role, based on priority, etc. The options here are literally infinite. Just make sure it makes sense for the business.
The script really only has 2 lines – that’s all that’s necessary for this powerful example.
When you’re writing a business rule like this, always make sure that you’re passing the parameter of true, through the method. If you leave this part out, it won’t work as expected. Alternatively, you could also probably find a use case for running current.setAbortAction(false), which would ALLOW the insert. But that would be on a very specific use case.
When the above condition is met, because it is true – we’ll process the script.
We first prohibit/stop/abort the attempted action and then throw up an informational message on the client when the form reloads. That’s all there is to it. No need to over complicate a simple process.
To Test The Business Rule – Go To An Incident
Now to test it out, go to any incident and try to modify the Assignment Group, but don’t add a work note. So we meet the condition of the business rule that we just created.
You’ll see the following error.
If you don’t, go back up and take another look at the business rule or add a comment to this post below and we can help you out.
And if you refresh the page, you will see that the line: “current.setAbortAction(true)” actually works. Because the Assignment Group is cleared out, which means that your attempt at updating the Assignment Group did not work – the update was never made and it was stopped by your business rule.
To complete the feature here – try changing the Assignment Group and ADD a work note. You’ll see that you’re not met with the error and that the record update is allowed. This is because your script is returning false, as the condition. is not being met – therefore the script is never being executed.
Congratulations!
You’ve successfully stopped an attempt by a user to update an Incident record.
While the example may not fit your business, take it an run with it.
Get creative with your use of this script and you’ll see how such a small piece of code can become very powerful.
You can read about this directly on the ServiceNow Docs page.
[…] Prohibit An Insert or Update with setAbortAction(true) […]
[…] Prohibit An Insert or Update with setAbortAction(true) […]