When using deleteMultiple() to cascade delete, prior calls to setWorkflow() on the same GlideRecord

Joshua Prakash
Tera Expert

Scenario:

Imagine you have a Purchase Orders table (x_purchase_order) in ServiceNow. Each purchase order record has associated Approval records in another table (x_purchase_approval). The system has a workflow that runs when a purchase order is deleted, sending notifications to relevant approvers and managers.

 

Case 1: Using deleteMultiple() with setWorkflow(false)

You want to delete all purchase orders that are in a "Cancelled" state, and you want to bypass workflows (e.g., no notifications should be sent) while deleting these records

 

var gr = new GlideRecord('x_purchase_order');
gr.addQuery('state', 'Cancelled');
gr.setWorkflow(false); // Supposedly to prevent the workflow from running
gr.query();
gr.deleteMultiple(); // This deletes all matching records

 

Expected Result: You expect the purchase orders to be deleted and the associated workflows not to run (i.e., no notifications sent).

 

Actual Result: The deleteMultiple() call ignores the setWorkflow(false) call. This means the deletion of records happens directly in the database, but because deleteMultiple() doesn't respect workflows, they wouldn't have run anyway. The key takeaway is that this happens regardless of your setWorkflow() statement.

 

Case 2: Using deleteRecord() with setWorkflow(false)

Now, let's say you still want to delete all "Cancelled" purchase orders, but this time, you're ensuring that workflows do not run (e.g., no notifications should be sent) while deleting these records, and you need to maintain more granular control.

 

You rewrite the script as:

 

var gr = new GlideRecord('x_purchase_order');
gr.addQuery('state', 'Cancelled');
gr.setWorkflow(false); // Prevent workflows
gr.query();

while (gr.next()) {
gr.deleteRecord(); // Deletes one record at a time, respecting setWorkflow
}

 

Expected Result: Each record is deleted one at a time, and workflows are respected. In this case, the system won't trigger the workflow because you've explicitly disabled it with setWorkflow(false).

 

Actual Result: The purchase orders are deleted, and because setWorkflow(false) is applied, the workflows associated with each deletion are not triggered. If you had set setWorkflow(true), the workflows would run for each deletion.

 

Key Difference:

  • deleteMultiple(): It ignores setWorkflow() completely. It bypasses workflows, business rules, and auditing, and deletes all matching records in a single database operation.
  • deleteRecord(): It respects setWorkflow(). If you set setWorkflow(false), workflows are not triggered. If setWorkflow(true) is set, workflows are triggered on a per-record basis as each record is deleted.

In summary, use deleteRecord() if you want to control workflow behavior during deletions.

Use deleteMultiple() when you want fast, bulk deletions and don't care about workflows or business rules being triggered.

0 REPLIES 0