Autorun AI Docs

6. Bulk Operations

Three functions write many records in one call. All of them accept an options object with skipTriggers, and all of them return the same result shape.

Use these instead of a loop

Calling create in a for loop issues one round trip per record and will hit the process timeout on any meaningful volume. The bulk functions do the work in a single call and report per-record outcomes.


The shared result shape

Prop

Type

{
  "success": false,
  "successCount": 2,
  "failureCount": 1,
  "results": [
    { "index": 0, "success": true,  "id": 201,  "message": "Record created successfully", "errorCode": null },
    { "index": 1, "success": true,  "id": 202,  "message": "Record created successfully", "errorCode": null },
    { "index": 2, "success": false, "id": null, "message": "Price is required", "errorCode": "VALIDATION_ERROR" }
  ]
}

index refers to the record's position in the array you passed in, which is how you match a failure back to its input.

const result = await context.functions.createBulk("lineitems", lineItems);

if (!result.success) {
  result.results
    .filter(r => !r.success)
    .forEach(r => console.error("Row " + r.index + " failed: " + r.message));
}
A partial failure still resolves

Bulk calls do not throw when some records fail — the successful ones are written and success comes back false. Ignoring the result leaves you with a half-applied batch and no indication anything went wrong.


createBulk(slug, records, options?)

async · returns the shared bulk result

Prop

Type

const result = await context.functions.createBulk("lineitems", [
  { InvoiceId: 101, Product: "Widget A", Qty: 3, Price: 19.99 },
  { InvoiceId: 101, Product: "Widget B", Qty: 1, Price: 49.99 },
  { InvoiceId: 101, Product: "Widget C", Qty: 5, Price: 9.99 }
]);

log("Created " + result.successCount + " line items");

const newIds = result.results.filter(r => r.success).map(r => r.id);

updateBulk(slug, records, options?)

async · returns the shared bulk result

Each entry is a flat record carrying its own ID alongside the fields to change.

Prop

Type

await context.functions.updateBulk("orderlines", [
  { ID: 11, TotalCost: 150.00, CostRate: 50.00 },
  { ID: 12, TotalCost: 300.00, CostRate: 75.00 },
  { ID: 13, TotalCost: 90.00,  CostRate: 30.00 }
]);

Both ID and id are accepted. System fields in the payload are stripped, so passing a whole record you just read is safe.

Entries without an ID are skipped in silence

A record with neither ID nor id is dropped before the batch runs. It produces no error and no entry in results, so successCount can legitimately be lower than the number of records you passed. Compare against records.length if that matters.

There is no "update everything matching a filter" form. Read the IDs first:

const unread = await context.functions.get("notifications", {
  filter: { field: "UserId", operator: "equal", value: context.claims.userId },
  pagination: { skip: 0, take: 1000 }
});

await context.functions.updateBulk("notifications",
  unread.records.map(r => ({ ID: r.ID, IsRead: true }))
);

deleteBulk(slug, idsOrOptions, options?)

async · returns the shared bulk result

Two call forms.

Pass an array of IDs.

await context.functions.deleteBulk("logs", [101, 102, 103]);

// with options
await context.functions.deleteBulk("logs", [101, 102, 103], { skipTriggers: true });

Pass an object with filter, ids, or both. The filter is resolved to matching IDs before deleting, and skipTriggers goes on the same object.

await context.functions.deleteBulk("sessions", {
  filter: { field: "ExpiresAt", operator: "lt", value: new Date().toISOString() },
  skipTriggers: true
});

The filter accepts the same single-condition and group shapes as get.

await context.functions.deleteBulk("drafts", {
  filter: {
    condition: "AND",
    predicates: [
      { field: "Status", operator: "equal", value: "draft" },
      { field: "ModifyDate", operator: "lt", value: "2025-01-01" }
    ]
  }
});
The filter form is not limited to a page

deleteBulk with a filter resolves every matching row, ignoring the 100-record read default. Verify the count with get before running a destructive filter.

const doomed = await context.functions.get("sessions", {
  filter: { field: "ExpiresAt", operator: "lt", value: new Date().toISOString() },
  pagination: { skip: 0, take: 1 }
});
log("About to delete " + doomed.totalCount + " sessions");

skipTriggers

By default each record in a batch fires its own create, update, or delete processes. { skipTriggers: true } suppresses them.

Reach for it when you are importing data, back-filling a column, or writing from inside another process and want to avoid recursion. Leave it off when the per-record processes carry business logic that must run — validation, downstream notifications, computed values.

/** @event afterCreate @entity invoices */
// Seeding child rows — no need to re-run their own processes
await context.functions.createBulk("invoicelines", lines, { skipTriggers: true });