Batch save operation

I have a Batch job where I want to archive a parent object, and all of the children objects of that parent, where children exist in multiple models. Can I add the child objects in the same batch job with the parent, given that I am only calling the “save”? Or do all objects in a Batch need to be from the same model?

Hey @mwalton

You can combine different operations across multiple models in the same Batch operation.

For example

var batch = new DB.Batch();

// parent model operation
var parent = DB.parent.first();
parent.archived = true;
batch.save(parent);

// child model operation
var children = parent.children.toArray();
for (var i = 0; i < children.length; i++) {
  var child = children[i];
  child.archived = true;
  batch.save(child);
}

// and one more operation on an unrelated model for good measure
var discardedObject = DB.temporary_data.first();
batch.destroy(discardedObject);

// one Batch execute to rule (execute) them all
batch.execute();

Hope that helps

It does, thank you Tielman