Script Execution Order¶
A workflow instance runs scripts and rules at fixed points of its life. Use this page to decide where a piece of logic belongs: which surface runs at the moment you need, whether it runs on the server or in the browser, and what it can change.
For the objects available in each surface, see Script Contexts.
Overview¶
| Moment | Surface | Runs on | Typical use |
|---|---|---|---|
| Instance is created | Opening script | Server | Set initial values for the whole instance. |
| Task is created | Prework script | Server | Prepare data, set deadlines, or select an action without user interaction. |
| Task is created, no action selected | Auto-select rule | Server | Select an action when a condition on the data is met. |
| User task is assigned | Role decision table or expression | Server | Resolve who receives the task. |
| Module task runs | Module handler, such as the automation script | Server | Run the automated operation and select an action. |
| User works on the form | Form scripts, validation and formatting rules | Browser | React to user input and enable actions when the data is valid. |
| User selects an action | User select script | Browser | Interact with the user at the moment of the decision, for example to sign files. |
| Action is accepted | Postwork script | Server | Apply the effects of the selected action and enforce mandatory checks. |
| Next steps are determined | Route validation rules | Server | Choose which routes of the selected action are followed. |
| Instance completes normally | Closing script | Server | Export or finalize the instance data. |
| Instance is terminated | Termination script | Server | Revert or clean up the effects of the instance. |
User Task¶
- The prework script runs. If it sets
$WorkItem.SelectedAction, the task is completed immediately and the flow continues at step 7. - Auto-select rules are evaluated in action order. The first rule that returns
trueselects its action and the flow continues at step 7. - The role resolves the participants and the task appears in their work lists.
- A user opens the task. Form scripts and rules run in the browser while the user works. Actions whose validation group fails stay disabled.
- The user selects an action and confirms it. The user select script runs and can cancel the selection or change the form data.
- The form data is submitted with the selected action.
- The postwork script runs with the selected action.
- The route validation rules of the selected action are evaluated and the next tasks are created. Each new task starts again at step 1.
An action can also be selected without a user: by a deadline, by bulk selection, by an email reply, or through the REST services. In these cases steps 4 and 5 are skipped and the flow continues at step 7.
Module Task¶
- The prework script runs and can select an action.
- Auto-select rules are evaluated.
- If no action is selected yet, the module handler runs, for example the automation script or the event operation. A module that waits, such as an event listener or a sub workflow, continues when its condition is met.
- After an action is selected, the postwork script and the route validation rules run as for a user task.
Instance Start and End¶
The opening script runs once when the instance is created, before the prework script of the start task.
The closing script runs when the instance completes normally, after the postwork script and the routes of the last task. The termination script runs instead when the instance is terminated.
Choosing the Right Surface¶
- Put logic that must always apply on the server: prework, postwork, routes, or the instance scripts. Browser scripts run only when a user works on the form.
- Use the user select script only for interaction with the user, such as confirmations or electronic signatures, and check the result again on the server when it matters.
- Use rules (auto-select, role, route, validation) for conditions. Keep them observational and use scripts for changes.
- Use the postwork script, not the prework script of the next task, for effects of a specific action. The postwork script knows which action was selected.