Sweep unpaid orders every morning

A scheduled automation that finds orders still unpaid after 3 days, tags them and sends the order numbers as a notice. A validated definition using the clock trigger, a fetch step and a bulk tag action.

Every morning, list the orders still unpaid after 3 days, tag them, and tell the store's managers which ones. This is the usual chase list for bank transfers and other manual payments. Trigger: schedule.fired@1. Steps: fetch, bulk_add_order_tags@1, notify_me@1.

Model background: Automations.

The definition

{
  "schemaVersion": 1,
  "trigger": {
    "type": "schedule.fired@1",
    "origins": ["SYSTEM"],
    "schedule": {
      "frequency": "daily",
      "time": "09:00"
    }
  },
  "steps": [
    {
      "id": "f1",
      "type": "fetch",
      "source": "orders",
      "where": {
        "financialStatus": ["pending"],
        "olderThanDays": 3
      },
      "limit": 50
    },
    {
      "id": "tagall",
      "type": "action",
      "contract": "bulk_add_order_tags@1",
      "input": {
        "orderIds": {
          "$ref": "steps.f1.ids"
        },
        "tags": ["unpaid-3d"]
      }
    },
    {
      "id": "note",
      "type": "action",
      "contract": "notify_me@1",
      "input": {
        "title": "{{steps.f1.count}} unpaid orders older than 3 days",
        "message": "Orders: {{steps.f1.numbers}}"
      }
    }
  ]
}

Replace before saving: time (in the store's time zone), the olderThanDays value (1 to 365), and the tag.

How it works

  • schedule.fired@1 runs by the clock, with no data. origins must include SYSTEM. schedule is one of { "frequency": "hourly", "minute": 0 }, { "frequency": "daily", "time": "09:00" }, { "frequency": "weekly", "weekday": 1, "time": "09:00" } (0 is Sunday) or { "frequency": "monthly", "dayOfMonth": 1, "time": "09:00" } (1 to 28). A missed occurrence fires once when the platform is back, never retroactively.
  • Under the clock there are no facts to filter on. You choose records with a top-level fetch step. where needs at least one filter. Here: financialStatus: ["pending"] and olderThanDays: 3. A fetch publishes steps.f1.ids, .numbers (order numbers), .count and .truncated. It never returns an order row, name, email or total. Cancelled orders are left out unless where.status names CANCELLED.
  • bulk_add_order_tags@1 takes orderIds of exactly { "$ref": "steps.f1.ids" }, never a typed list, and tags every fetched order in one change. The fetch that feeds a bulk action has a limit of at most 50.
  • notify_me@1 may print {{steps.f1.count}} and {{steps.f1.numbers}}.

What it does not do

  • The fetch cannot filter on payment method, so it covers every unpaid order, not only bank transfers. In practice most of what stays pending for 3 days is a manual payment.
  • It takes the oldest 50. A tagged order is still unpaid tomorrow, so with more than 50 orders waiting, the same oldest 50 come back every day until they are paid or cancelled, and steps.f1.truncated is true. Clear the backlog first, or run it with a smaller olderThanDays once it is under control.
  • It never cancels. Cancelling would need cancel_order@1, which skips an order that is paid or has a payment waiting.
  • For a table in an email instead of a notice, send_internal_email@1 under the clock accepts an input.report and sends nothing when the table is empty (NOTHING_TO_REPORT).

Create it

With the Admin MCP:

  1. get_automation_guide with topic: "clock".
  2. validate_automation_definition. Fix every finding.
  3. test_automation with entity: { "type": "Store", "id": "<the store id>" }. It reads the list right now, shows which orders it would tag and how the notice would read, and writes nothing.
  4. create_automation_draft with name: "Unpaid orders sweep".
  5. Tell the merchant to approve it in the dashboard.

With the SDK:

// The clock is tested on the Store; entity.id is the store id.
const result = await admin.testAutomation(storeId, {
  definition,
  entity: { type: 'Store', id: storeId },
});

const draft = await admin.createAutomation(storeId, { name: 'Unpaid orders sweep', definition });

What the merchant sees

A notice each morning such as "4 unpaid orders older than 3 days" with the order numbers, and the tag unpaid-3d on those orders so they can filter the orders list. The history shows one run per day.