Workflows

Configure workflow fields

Declare workflow, stage, and activity fields, configure initial values and validation, and manage role-constrained assignments.

Early access

Fields hold the data a workflow instance carries: the document it is about, values supplied at start, values computed from your content, and decisions people record along the way. They use the same model as fields in a Sanity schema (a name, a type, and optional validation), but the values live on the workflow instance, not on a content document.

This guide adds five fields to the quick start’s article-review workflow. You declare fields at workflow and stage scope, and fill them from a start value, a GROQ query, and another field. Then you read a field in a transition condition and make it editable, so the instance finishes only after legal sign-off.

Prerequisites

  • The finished quick start: workflows/article-review.ts deployed through the dev deployment in sanity.workflow.ts, and the _id of the published article you started it with.
  • The engine module from Run the engine from your own code (scripts/engine.ts) and a SANITY_AUTH_TOKEN with write access to the dataset. The CLI has no command for editing a field, so the last step uses a short script.

Decide where each field lives

When a field gets its value and how long it lasts depend on where you declare it:

  • Workflow fields, in defineWorkflow({fields}), resolve when the instance starts and last for its lifetime.
  • Stage fields, in defineStage({fields}), resolve each time the instance enters that stage and belong to that stage visit.
  • Activity fields, in defineActivity({fields}), also resolve on stage entry and belong to one activity.

Conditions read the fields in scope as $fields.<name>. A transition’s condition sees workflow fields and stage fields, but not activity fields. So declare a field that a transition reads at workflow or stage scope.

Two scopes can declare a field with the same name. The narrower scope wins: activity, then stage, then workflow. The same order applies when you name a field without a scope, in these places:

  • An operation or a field edit that leaves out the scope.
  • An effect handler that uses a bare field name. The search starts from the activity whose action queued the effect.

Declare and seed the fields

Add three workflow fields and two review stage fields to workflows/article-review.ts. Every field declares a type; Field types lists them all. To narrow string, text, number, or progress values, add validation; see Options and validation.

import {
  defineAction,
  defineActivity,
  defineField,
  defineStage,
  defineTransition,
  defineWorkflow,
} from '@sanity/workflows/define'

export const articleReview = defineWorkflow({
  // ...
  fields: [
    defineField({
      type: 'subject',
      name: 'subject',
      // ...
    }),
    defineField({
      type: 'string',
      name: 'priority',
      title: 'Priority',
      initialValue: {type: 'input'},
      options: {
        list: [
          {title: 'Standard', value: 'standard'},
          {title: 'Urgent', value: 'urgent'},
        ],
      },
    }),
    defineField({
      type: 'string',
      name: 'articleTitle',
      title: 'Article title',
      initialValue: {type: 'query', query: '*[_id == $fields.subject.id][0].title'},
    }),
    defineField({type: 'boolean', name: 'legalApproved', title: 'Legal approved', editable: true}),
  ],
  stages: [
    // drafting is unchanged
    defineStage({
      name: 'review',
      // ...
      fields: [
        defineField({type: 'dueDate', name: 'reviewDue', title: 'Review due', editable: true}),
        defineField({
          type: 'string',
          name: 'reviewPriority',
          title: 'Review priority',
          initialValue: {type: 'fieldRead', field: 'priority', scope: 'workflow'},
        }),
      ],
      // ...
      transitions: [
        defineTransition({
          name: 'to-approved',
          title: 'Approve and finish',
          to: 'approved',
          when: '$allActivitiesDone && $fields.legalApproved == true',
        }),
      ],
    }),
    // approved is unchanged
  ],
})

Each field shows a different way to get a value. A field without initialValue starts empty and waits for an action or an edit to fill it in, as legalApproved and reviewDue do. Initial-value sources lists the complete forms.

  • priority uses {type: 'input'}, so you supply it when you start the instance. options.list restricts it to the listed values, and the engine checks that list on every write. Only workflow fields can take an input, and only workflow input fields can set required: true.
  • articleTitle uses {type: 'query'}. The engine runs the GROQ query against the Content Lake when the field enters scope. Keep these rules in mind:
  • A query can read fields declared earlier in the same scope, so declaration order matters.
  • In a query, $fields.subject is the stored reference {id, type}, so read .id. Conditions see the loaded document instead, so they read ._id.
  • A query can use GROQ clock functions such as now(). It also receives $now.
  • reviewPriority uses {type: 'fieldRead'} with scope: 'workflow'. It copies the workflow field priority into the stage visit. A field read copies the stored value only. It doesn’t load the referenced document, so use a query when you need a property of the article.
  • reviewDue is a dueDate: the deadline for the stage visit. Each scope can declare at most one dueDate or dueDatetime field, and it must be a top-level field.

The to-approved transition reads $fields.legalApproved. A transition without when waits for $allActivitiesDone. Once you add a when, the default no longer applies, so the condition includes $allActivitiesDone and adds the field check. Write GROQ conditions covers the rest of the condition language.

Deploy and start an instance

Instances keep the definition version they started with, so deploy the change and start a new instance. Pass the input field with a second --field:

npx sanity-workflows deploy
npx sanity-workflows start article-review \
  --field subject='{"id":"dataset:PROJECT_ID:DATASET_NAME:DOCUMENT_ID","type":"article"}' \
  --field priority=urgent

If the start value isn’t in options.list, the start fails with FieldValueShapeError before the instance is created. The message starts with Field entry value shape invalid for "priority" (string) and lists the allowed values.

Fire the two quick start actions with the instance ID the start command printed:

npx sanity-workflows fire-action INSTANCE_ID --activity write --action submit
npx sanity-workflows fire-action INSTANCE_ID --activity sign-off --action approve

Verify the seeded values

Run npx sanity-workflows show INSTANCE_ID. The instance stays in review with every activity done, because legalApproved is still empty:

article-review v2  dev.wf-instance.a1b2c3d4e5f6
  Stage:     review
  Started:   2026-10-26 19:34:21
  Completed: —
  Partition: dev

⚠ STUCK — no exit transition's trigger is satisfied

Why it's stuck:
ℹ every activity is resolved, but no exit transition's `when` is true.
Likely a routing state value a trigger reads never got written.

Suggested fix:
  • tick — Fix the content or fields the intended transition needs, then call workflow.tick from code to re-check its transitions (the CLI has no tick command); the next commit or a deployed heartbeat also re-checks them.
  • abort — Abort the instance if it should not continue.

Stage history:
  • drafting (exited 2026-10-26 19:34:21)
      ✔ write  [done]
  • review (current)
      ✔ sign-off  [done]

Add --json to print the instance document. Its fields array holds the workflow fields, with priority set to urgent, articleTitle set to the article’s title, and legalApproved set to null. The current entry in stages holds the stage fields, with reviewPriority copied as urgent.

Make a field editable

By default, a field can’t be edited directly. Only actions can write it. legalApproved sets editable: true, so any caller can set it while the field is in scope. editable takes one of three forms:

// Anyone can edit while the field is in scope.
defineField({type: 'boolean', name: 'legalApproved', title: 'Legal approved', editable: true})

// A string array lists the project roles that can edit.
defineField({type: 'boolean', name: 'legalApproved', title: 'Legal approved', editable: ['legal']})

// A string is a GROQ condition.
defineField({
  type: 'boolean',
  name: 'legalApproved',
  title: 'Legal approved',
  editable: "$fields.priority == 'urgent'",
})

These rules apply to editable:

  • A role list can’t be empty.
  • An edit condition can read $actor, $assigned, $can, $attributes, and $fields.
  • Editing a field without editable throws EditFieldDeniedError, for example Field "workflow:priority" is not editable: field is not declared editable.

Like action filters, these checks are advisory. They help your interface disable controls and explain why. The Content Lake is the only place a rule is enforced. Actors and enforcement explains how to enforce one.

Edit the field from code with engine.editField. Create scripts/approve-legal.ts:

import {engine} from './engine'

const instanceId = process.argv[2]
if (!instanceId) throw new Error('Usage: npx tsx scripts/approve-legal.ts INSTANCE_ID')

const result = await engine.editField({
  instanceId,
  target: {field: 'legalApproved'},
  value: true,
})

console.log(`legalApproved set. Now at ${result.instance.currentStage}`)

Run the script with the instance ID. A field edit goes through the engine like an action does. The engine checks editable, saves the change and a history entry in one transaction, and then re-evaluates the instance. So one edit can make an action available, satisfy a requirement, fire a triggered action, or move the instance to another stage. Here, setting legalApproved satisfies to-approved in the same call:

npx tsx scripts/approve-legal.ts INSTANCE_ID
legalApproved set. Now at approved

Run npx sanity-workflows show INSTANCE_ID again to confirm the instance is complete in approved. Editors can also set editable fields from Sanity Studio once you add Workflows to Studio.

Edit one row of a list

Each row of an array field, including todoList and notes, can carry a _key. The engine adds one to every row an append writes, and Studio adds one to every row it saves. Add a row’s key to the target to edit that row instead of the whole list. Read the keys from the instance with npx sanity-workflows show INSTANCE_ID --json.

These examples use the engine from approve-legal.ts on a running instance of a workflow that declares this field, with rows keyed k1, k2, and k3:

defineField({type: 'todoList', name: 'checklist', title: 'Checklist', editable: true})
// Mark one row done. The row keeps its _key.
await engine.editField({
  instanceId,
  target: {field: 'checklist', key: 'k2'},
  value: {label: 'Check images', status: 'done'},
})

// Move a row before another row.
await engine.editField({
  instanceId,
  target: {field: 'checklist', key: 'k3'},
  mode: 'move',
  value: {before: 'k1'},
})

// Remove a row.
await engine.editField({
  instanceId,
  target: {field: 'checklist', key: 'k1'},
  mode: 'unset',
})

With a key, the modes work on that row:

  • set replaces the whole row, so pass every column you want to keep. A value that carries a different _key is refused.
  • unset removes the row.
  • move places the row before or after another row, with {before: key} or {after: key}.

append adds a row and takes no key. The engine applies a row edit to the list as it is stored when the edit commits, so edits by two people to different rows are both kept. If the key, or the row a move is placed against, no longer names exactly one row, the edit throws ContractViolationError and saves nothing. This happens when someone else removed the row, for example. A row edit is checked, gated, and recorded in history like a set of the whole list. Fields of type doc.refs and assignees take no key and are replaced whole.

Assign work to people and roles

assignee and assignees fields hold people and project roles. On an activity, they set who $assigned matches, and roles restricts who can be assigned. Let reviewers claim an assignment adds one to the review stage and lets an editor take the review.

Next steps

Visiting agent?

Was this page helpful?

Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change site theme