Run workflows
A workflow is a list of steps that find, create, update and delete records together: every step's change lands, or none does. You declare it once in a file. Your app then starts it with a single call to the generated API, which returns a run at once. The run happens in the background, as whoever made the call. Run the commands in your app's directory, after multipaas init, which Build on the generated API covers.
A workflow is part of your model. Declaring, changing and deleting one needs permission to edit the model, and each makes a new API version.
Write the definition
A workflow's definition is one JSON file. This one closes a ticket, so it needs a Ticket entity whose status field is an enum with the value CLOSED. Model your data shows how to add one.
{
"key": "closeTicket",
"name": "Close a ticket",
"description": "Closes a ticket",
"trigger": {
"kind": "onDemand",
"inputs": [{ "key": "ticketId", "type": "ID", "required": true }]
},
"runAs": { "kind": "starter" },
"steps": [
{
"key": "ticket",
"kind": "find",
"name": "Find the ticket",
"description": "Looks the ticket up by id",
"entity": "Ticket",
"where": { "id": { "eq": { "ref": "trigger.input.ticketId" } } },
"limit": 1
},
{
"key": "close",
"kind": "update",
"name": "Close it",
"description": "Sets the status to closed",
"entity": "Ticket",
"target": { "each": "ticket" },
"values": { "status": "CLOSED" }
}
]
}
Entities are named by their GraphQL type name and fields by their GraphQL field name, as multipaas model show prints them. Every property the file doesn't list below is refused.
keystarts with a lowercase letter, then letters and digits, at most 63 characters. It names the mutation your app calls, and is unique among the application's workflows.nameanddescriptionare plain text for the people and agents who read them: 1 to 200 and 1 to 1000 characters.triggeris{"kind": "onDemand", "inputs": [...]}. Up to 50 inputs, each with akey, atypeofID,String,Float,BooleanorDateTime, andrequired(defaultfalse).runAsis{"kind": "starter"}: the workflow runs as whoever made the call.stepsholds 1 to 20 steps, run in order. Each has akey, aname, adescription, akindand anentity.findreads records. It takes an optionalwhere, one optionalorderBy, a list holding at most one{"field": FIELD, "direction": DIRECTION}withDIRECTIONasc(the default) ordesc, such as[{"field": "createdAt", "direction": "desc"}], and an optionallimit. A condition inwhereis{"FIELD": {"OPERATOR": VALUE}}.FIELDcan also beid,createdAtorupdatedAt. Which operators a field takes depends on its kind. They are the ones the generated API's filter offers for it, except thatidalso takesisNull, which that filter does not:- a text field takes
eq,in,contains,startsWith,endsWithandisNull; - a number or timestamp field,
createdAtandupdatedAttakeeq,lt,lte,gt,gteandisNull; - a boolean field takes
eqandisNull; - an enum or reference field and
idtakeeq,in,notInandisNull; - a text or enum list field takes
hasandisNull.
Any other of these operators (
eq,lt,lte,gt,gte,in,notIn,isNull,contains,startsWith,endsWith,has) is refused withOPERATOR can't be used on FIELD; use OPERATORS, naming the ones the field takes;some,none,isand any other name getonly value operators are supported yet.inandnotIntake a list, and a single value given to one is refused within takes a listornotIn takes a list. Every other operator takes one value, and a list given to one is refused withOPERATOR takes one value, not a list; foreqthe message adds; use in to match any of several. The records come inorderByorder, solimittakes the first N in that order andsteps.STEP_KEY.firstis the first of them. WithoutorderBythe order is unspecified. A find reads its records in batches, each as of that batch's own read, so records another call changes while the run is going can move between batches: a record is found at most once, but one that moves can be missed, andsteps.STEP_KEY.firstis the first record of the first batch as that batch read it. A value given to any operator butisNullin a condition, including arefwhose type the definition fixes, must be of the field's type (forhas, of the list's element type; forinandnotIn, each element of the list must be of the field's type): a number for a number field,trueorfalsefor a boolean field, and a string for any other field and forid,createdAtandupdatedAt. Otherwise the definition is refused withFIELD takes a number,FIELD takes a booleanorFIELD takes a string, or, for a ref to a list field,FIELD takes one value, not a list.nowfits only a timestamp field,createdAtandupdatedAtin a condition. Anywhere else in a condition it is refused withFIELD can't take now. A literal in a condition must also have the form its field needs:idand a reference field take a record id, and a timestamp field,createdAtandupdatedAttake a time. Anything else is refused withFIELD takes a record idorFIELD takes a time. Arefis checked the same way where the definition fixes its form. A timestamp field, aDateTimeinput andnoware times, and a reference field holds the id of a record of the entity it points at. So a time is refused where an id is needed, and an id where a time is needed. The id of one entity's record is refused where another entity's is needed, withFIELD takes the id of a TYPE. Arefto an enum field is refused with the same two messages when it is given to a reference field, a timestamp field,id,createdAt,updatedAtor a record target. Given to another enum field that shares none of its values, it is refused withFIELD shares no value with ENUMFIELD. An enum field whose values partly overlap is accepted.nullis refused as the operand of any operator exceptisNull, in aninornotInlist too, withFIELD can't be compared with null; use isNull.isNulltakes exactlytrueorfalse, on a field of any type and onid,createdAtandupdatedAt. A list, a string, arefor anowis refused withisNull takes a boolean. A value given for an enum field must also be one of the enum's values, or the definition is refused withX is not a value of FIELD.- a text field takes
createadds a record fromvalues, an object of field name to value. Itstenantis"trigger", the default: the record goes in the app-tenant the call was made in.updatechanges the records itstargetnames, tovalues.deletedeletes the records itstargetnames.A
targetis{"each": "STEP_KEY"}, every record an earlierfindstep found, or{"record": VALUE}, the one record whose id the value gives. That value must be a record id. A list,null, a number, a boolean,nowor a string that isn't a record id is refused withrecord takes one value, not a list,record takes a string,record can't take noworrecord takes a record id. Arefto the id of another entity's record is refused withrecord takes the id of a TYPE.
A value is a string, number, boolean or
null,{"ref": "REFERENCE"}, or{"now": {"plus": DURATION, "minus": DURATION}}for a time relative to the run (either part, both or neither:{"now": {}}is the run's own time). A duration is an object with any ofdays,hoursandminutes, each a whole number, such as{"days": 1, "hours": 2}. Leave out the parts you don't need.plusandminusare each at most 36,500 days, and so is the time they give together (plusminusminus). A longer one is refused withnow can be at most 36,500 days from the run's time.A value for a list field is instead a list of up to 1,000 strings, such as
["urgent", "billing"], with nonull,refornowin it. For an enum list field, each string must be one of the enum's values. The empty list[]is allowed.In
values, a value whose type the definition already fixes must fit its field:- an
ID,StringorDateTimeinput is a string; nowis a time, and fits only a timestamp field or a text field, which stores it as an ISO string. Anywhere else it is refused withFIELD can't take now;countand aFloatinput are a number;- a
Booleaninput is a boolean; - none of these is a list;
steps.STEP_KEY.first.FIELDanditem.FIELDare a string, number or boolean, and a list or not, as that field is.
A value that doesn't fit is refused with
FIELD takes a list,FIELD takes one value, not a list,FIELD takes a number,FIELD takes a boolean,FIELD takes a string,FIELD can't take now,FIELD takes a record id,FIELD takes a timeorFIELD takes the id of a TYPE. Invalues, a literal for a reference field must be a record id, and one for a timestamp field must be a time. A list over 1,000 strings is refused withFIELD holds at most 1,000 values. Only what the definition doesn't fix waits for the run:- the string a
StringorIDinput or a text field holds; - a value of one enum given to another enum field whose values only partly overlap;
- a time or a record id (a
DateTimeinput, or a timestamp or reference field) given to an enum field, since an enum's values can be any text, a time or an id among them; - whether a
refcomes out empty.
A time is an ISO 8601 string with seconds and
Zor±HH:MM, such as2026-01-31T09:30:00Z, and fractional seconds are allowed. A record id is a UUID.In a condition:
- A string that is no record's id, on
idor on a reference field, matches no record undereqandinand every record undernotIn. So does a string that is no value of an enum field. - An empty value fails the step with
FIELD's condition value is empty; use isNull to match no value. - A string that isn't a time, on a timestamp field,
createdAtorupdatedAt, fails the step withFIELD takes a time.
In
values, and as a record target, a string that isn't an enum's value, a record id or a time fails the run.- an
A reference is
trigger.input.KEYfor an input,steps.STEP_KEY.countfor how many records an earlierfindstep found,steps.STEP_KEY.first.FIELDfor a field of the first record it found, oritem.FIELDfor a field of the record being changed.item.works only in anupdateordeletestep whose target iseach, over afindstep on the same entity. A reference to a file or related field is refused withFIELD is a file field, which a workflow can't readorFIELD is a related field, which a workflow can't read.
A workflow can't set a file or related field. Text the database can't store, a NUL character or an unpaired surrogate, in a name, a description or a literal in values or where, is refused as a problem at its path with must not contain a NUL character or an unpaired surrogate.
Not supported yet
A definition that uses one of the other kinds the model knows is refused with its own message:
- a
scheduleordataChangetrigger:only onDemand workflows are supported yet - a
runAsof kindownRole:only starter workflows are supported yet - a
createstep'stenantof"appWide"or{"sameAs": "STEP_KEY"}:only the trigger tenant is supported yet - a condition operator other than the ones listed above, such as
some,noneoris:only value operators are supported yet - a reference to
trigger.recordortrigger.previous:the onDemand trigger supplies no record
Any other kind or value is refused as not valid, with the message of the property it is on.
Declare it
multipaas workflows create --file closeTicket.json
A definition that doesn't hold together is refused with WORKFLOW_INVALID, and the CLI prints every problem as path: message, such as steps[1].target.each: ticket is not an earlier find step on Ticket. Fix them all and run the command again.
multipaas workflows list and multipaas workflows show KEY read workflows back. To change one, edit the file and run multipaas workflows update KEY --file closeTicket.json: the workflow keeps its id, and if the file's key is different, that becomes its new key. multipaas workflows delete KEY deletes it.
A change to the model is refused when a live workflow would no longer resolve against it, such as removing a field the workflow uses, or changing a field's type so a condition no longer fits. The message names the workflow. Removing an entity or field it uses ends workflow KEY uses a field or entity this change removes; update or delete the workflow first. Adding a required field with no default, making a field required or removing a required field's default, while a create step leaves that field out or a step sets it to null, says workflow KEY step STEP would leave required FIELDS empty; update or delete the workflow first. Any other change the workflow no longer fits says workflow KEY no longer fits this model (WHY); update or delete the workflow first. A run uses the model at the API version the call is made on. To get past the required-field refusal, change what the named step does with the field, then retry the change. Where the step sets the field to null, give it a value. Where a create step leaves it out, set it in the step's values. A field you are adding can't be named in a step yet, so add it with a default, or add it optional, set it in the step's values, and then make it required. A file field can't be set by a workflow, so it can't be required while a create step writes its entity: remove that step, or leave the field optional. Or delete the workflow. To get past the other refusals, update the workflow so it no longer depends on what is changing, or delete it.
An application with workflows can't have an entity or enum whose GraphQL type name is WorkflowRun or WorkflowRunStatus, or an entity whose plural name is WorkflowRun, which makes its generated query workflowRun, because the generated API uses those names. Declaring one in an application that has workflows, or the first workflow in an application that has one, is refused with WORKFLOW_RUN_NAME_CLASH. To clear it, give the entity or enum a different --type-name with multipaas entity create or update, or multipaas enum create or update, and give the entity a different --plural-name when the plural clashes. Changing only --name keeps the type name of one that already exists. Or delete the workflows.
An API version made before you update or delete a workflow keeps the mutation it had, with the definition and key the workflow had then. Only versions made after the change see it.
Let a caller run it
A workflow becomes a mutation in the generated API: run plus the key with its first letter capitalized, plus Workflow, so closeTicket becomes runCloseTicketWorkflow. It appears in the next API version made after you declare the workflow, so move your app to it first.
Call it with the workflow's inputs in one input argument. A workflow with no inputs takes input: {}.
mutation ($ticketId: ID!) {
runCloseTicketWorkflow(input: { ticketId: $ticketId }) {
id
workflowKey
status
failedStep
error
recordsChanged
createdAt
finishedAt
}
}
The caller needs the workflow.run permission for that workflow. Give a role that permission, using the workflow's id, the ID column of multipaas workflows list:
multipaas roles update --role ROLE_KEY --add-permission workflow.run=WORKFLOW_ID
A workflow id that is unknown, deleted or another application's is refused with workflow X is not a workflow of this application.
Run background jobs shows how to give a role to an app identity, and a person gets one when you invite them to an app-tenant.
What a run does
The call returns at once with the run: its id and workflowKey, and normally status QUEUED, with failedStep and error null, recordsChanged 0, createdAt set and finishedAt null, and nothing changed yet. A worker picks the run up and runs the steps in order in one transaction, in the background. Follow it by asking the generated API for the run by its id until status is SUCCEEDED or FAILED; QUEUED and RUNNING are not final. Ask from the same environment and app-tenant, as the same user, that started the run: workflowRun(id) answers null for any other.
query ($id: ID!) {
workflowRun(id: $id) {
status
failedStep
error
recordsChanged
finishedAt
}
}
Only the person or app identity who started a run, in the app-tenant it was started in, can read it. Anyone else, and an id that matches no run, gets null. workflowRun is in every API version that has a workflow.
- All or nothing. If any step fails, none of the run's changes land. The run ends with
statusFAILED,failedStepthe failing step'skey,errorits message andrecordsChanged0. A failure outside every step hasfailedStepnull, and itserroris the failure's message when the caller can act on it, elseunexpected error. A run that succeeds endsSUCCEEDEDwithrecordsChangedthe number of records it created, updated and deleted. Every run the call starts is written to the workflow's history, queued, succeeded or failed. The exception is an input the database can't store, text with a NUL character or an unpaired surrogate: the call is refused with a field error on that input before any step runs, and no run is written. A caller withoutworkflow.runon the workflow gets anUNAUTHORIZEDerror instead, and no run is written. - Never more than the caller may. Each create, update and delete goes through the same record services as the generated API's own mutations, as the caller, so the caller's permissions and the app-tenant's isolation apply to each one. A step the caller may not do fails the run. A
createorupdatestep also follows the generated API's rules for required fields at the API version the call is made on, and acreatestep fills a required field's default whenvaluesgives none. When you create or update a workflow, it is refused if acreatestep leaves out a required field that has no default (FIELD is required and has no default) or a step sets a required field tonull(FIELD is required, so it can't be null). A file field can't be set by a workflow, so acreatestep can't write an entity that has a required file field. A value that only comes out empty when the workflow runs, such as arefto an input the call left out or arefto a field of a find that found nothing, fails the run as a record target or for a required or not-null field invalues. For any other field invalues, it sets the field to no value. In a condition, an empty value fails the step withFIELD's condition value is empty; use isNull to match no value. Afindstep sees only the records the caller could list through the API: over records the caller can't read it finds none, and the run goes on. - Batches of up to 1,000. A
findstep reads 1,000 records at a time, and anupdateordeleteovereachwrites up to 1,000 at a time, and an update writes fewer when the records are large. Acreatestep creates one record. - A time budget. A run has about 14 minutes from when a worker starts it, and time spent
QUEUEDdoes not count. One that is still going when it is spent fails with the errorran out of time, and nothing lands. With records of up to about 1 KB of data, a run can update about 36,000 records or delete about 25,000. Larger records take longer to update or delete. Split a larger change withwhere, into several runs. - A monthly limit. Each run the call starts counts toward the plan's Workflow runs per month, except one recorded
WORKFLOW_NOT_STARTED, which is not counted. A run stays counted for the rest of the month even if its environment or application is erased. Once the account has started that many in a UTC month, the next call fails with the codeWORKFLOW_RUN_LIMIT_REACHEDand "This account reached its Workflow runs per month limit.", with the measureworkflow_runs_month,limitandresetsAtin itsextensions, and writes no run. Only workflow starts are refused untilresetsAt; the app's other requests carry on. The account's usage page shows the runs used this month. - A storage limit. While the account is 10% or more past its database storage limit, a
createorupdatestep that writes a record is refused as the generated API's own create and update are (see Limits): the run endsFAILEDwith that step asfailedStep, the error "This account reached its database storage limit." and nothing lands. Adeletestep is never refused this way. The limit doesn't reset on a schedule, so running it again fails the same way until the account is less than 10% past its limit. - The caller is checked twice. The call needs
workflow.run, and the worker checks it again when it starts the run. A caller who lost it in between gets aFAILEDrun and nothing changes. - A run that did not finish. A run that could not be queued is recorded
FAILEDwith the codeWORKFLOW_NOT_STARTED, and the call returns an error. A run whose worker stopped before it finished endsFAILEDwith the errorthe run stopped before it finishedand the codeWORKFLOW_STOPPED. - No retry. An on-demand run is tried once. When it fails, your app decides whether to call it again.
Read the run history
Every run, whether queued, running, succeeded or failed, is kept with the inputs the caller gave, who ran it, how many records it changed and, for a failed run, the step that failed and why. Read a workflow's runs in an environment, newest first:
multipaas workflows runs KEY
Without --env it reads the environment in your app's multipaas.json, or the default one when there is none. Add --env ENV_ID for another. Run history shows the inputs callers supplied, which are your app's data, so it needs permission to edit the model.