How to cancel a current record update from a workflow run script activity?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 03:08 PM
Hello Everyone,
If I have an asset table workflow and I reach a condition where the record update needs to be aborted, can I do that from a Run Script activity?
Example: Workflow turnstile has reached the set retries; the triggering record update needs to be aborted/cancelled. I have a working run script activity that cancels the workflow context but have not been able to figure out how to abort/cancel the triggering record update. Can it be done from the workflow or do I need a separate business rule to trigger on the workflow cancellation?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 03:44 PM
Yes, it is possible to abort the triggering record update from a Run Script activity in a workflow in ServiceNow.
To do this, you can use the setAbortAction() method of the GlideRecord object in the script. Here is an example script that cancels the record update:
var gr = new GlideRecord(current.table_name);
gr.get(current.sys_id);
// Cancel the record update
gr.setAbortAction(true);
When the setAbortAction() method is called with a value of true, it cancels the current record update and prevents any further updates from being made to the record.
You can add this script to a Run Script activity in your workflow and configure it to run when the condition for cancelling the record update is met.
Note that cancelling the record update in this way does not cancel the entire workflow. If you want to cancel the entire workflow when the record update is cancelled, you will need to add additional logic to the workflow or create a separate business rule to trigger on the workflow cancellation and cancel the workflow context.
Please mark my answer as a solution/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 03:58 PM
Thanks Punit. I will try that.
I have a working Run Script activity that cancels the workflow context:
var aFlow = new Workflow().getRunningFlows(current);
var aID = current.sys_id;
while (aFlow.next()) {
if (aFlow.workflow == 'workflow sys_id' && aFlow.id == aID) {
new Workflow().cancelContext(aFlow);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 10:40 AM - edited 04-26-2023 10:42 AM
Punit,
I created the activity with the following script:
var asset = new GlideRecord('hardware');
asset.get(current.sys_id);
asset.setAbortAction(true);
I see in the log where the abort of the update happened, but when I reload the asset page, it is showing the updates to the record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 02:33 PM
Based on the script you have provided, it seems that you are setting the abortAction property of the GlideRecord object to true. This property determines whether the GlideRecord object should abort the current database action if any error occurs during the update operation.
However, this property does not prevent any updates that have already been performed on the record. If there were any changes to the record made before the setAbortAction method was called, those changes will still be saved to the database.
To prevent any updates to the record, you can use the updateWithReferences method of the GlideRecord object with the all parameter set to false before calling setAbortAction method.
Here's an example of how you can modify your script to prevent any updates to the record
var asset = new GlideRecord('hardware');
asset.get(current.sys_id);
// Update the record with references, but don't update any referenced records
asset.updateWithReferences(false);
// Set the abort action to true
asset.setAbortAction(true);
With this modification, any updates made to the record before calling updateWithReferences method will not be saved to the database, and any subsequent updates will be aborted if an error occurs.
Please mark my answer as a solution/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit