What you will build
A Letter batch document stands for one mass mailing. Each letter generated for it is a Letter document whose BatchNo keyword holds the batch’s number, and whose SignedDate keyword is filled in once the recipient has signed. Someone opens a letter batch and chooses Withdraw letters. They are asked to confirm. A workflow then runs a script in the background that deletes every unsigned letter of the batch and keeps the signed ones. While it works, they see its progress. When it is done, they see how many letters were deleted and how many were kept. A Test run choice counts the letters without deleting anything. The example has three parts: a script, a workflow definition, and a document action. The names are illustrative; substitute your own document types and keywords.Prepare the configuration
You need a tenant with the workflow engine switched on, and:script-engine.manageto create and publish the script;workflow-engine.manageto build the workflow and to configure a workflow document action, plus document configuration access for Documents → Document actions.
Write the script
Create and publish a Workflow Action script namedWithdraw letters from Return a summary for a workflow message, with your own document type and keyword type IDs. Note its slug in the Scripts list.
Its contract is all the workflow relies on:
- Input:
document_id, the letter batch, andtest_run, true when nothing should be deleted. - Output:
summary, one line of text for the person, andlevel,successwhen every letter was withdrawn orwarningwhen signed letters were kept. - Failure: a batch document without a BatchNo makes the script fail, which sends the workflow down its error path.
Build the workflow
-
Open Settings → Admin Settings → Workflow → Designer and create a definition named
Withdraw letters. - Choose the Document anchor and scope it to Letter batch. Leave the triggers empty: the document action starts it.
- Add an initial node named Withdrawing, and two terminal nodes named Done and Could not withdraw. In the definition panel, choose Could not withdraw as the Error node.
-
On Withdrawing, open the Actions tab and add two actions, in this order:
-
Workflow.SetMessage—Levelinfo,TextWithdrawing letters…. The person sees this as progress while the script runs. -
Script.Run— your script’s slug inScriptSlug, RunInBackground on,IntoVariableundo, and thisInput, written in the formula editor:
-
-
Draw a Join transition from Withdrawing to Done whose job is
$instance.undo. -
On Done, add
Workflow.SetMessage. SwitchLevelto expression mode and enter$instance.undo.output.level, and setTextto{{instance.undo.output.summary}}. LeaveTitleempty: the person then sees the level’s heading, Done or Done, with warnings, which fits a test run as well as a real one. -
On Could not withdraw, add
Workflow.SetMessagewithLevelerror,TitleLetters not withdrawn, and the textNot all letters could be withdrawn. Run the action again, or ask your administrator to check the run.
- The choice arrives as
$instance.choice. The document action below puts the chosen value there, and the input turns it intotest_run. Without a confirmation,$instance.choiceis null, sotest_runis false. - The error node needs its own message. If the script fails, the run reaches Could not withdraw. The Withdrawing letters… message was set before the error path, so it is no longer shown; without a message on the error node, the person would see the action as done. You can add
{{instance._error.message}}to the text, but it is the failing step’s technical reason. {{owner.username}}is the person who clicked. Steps after the Join run as the system, so{{identity.username}}on Done would rendersystem. The run’s owner is the person who chose the action, in every step, so use{{owner.username}}in anything you add there later, such as a note.
Create the document action
Open Documents → Document actions and choose Add action.- Name it
Withdraw letters, choose Workflow Action, and select theWithdraw lettersworkflow. - Select the Letter batch document type, not Global, and the user group that may withdraw letters.
- Under Confirmation, switch on Ask the user before the workflow starts, and fill in:
- Title:
Withdraw the letters in this batch? - Message:
Letters that have not been signed are deleted. Signed letters are kept. - Choices: Withdraw with value
withdrawand style Destructive, and Test run with valuetestand style Secondary. - Variable for the choice: keep
choice.
- Title:
- Choose Preview to check the dialog, then save.
test exactly, so the Test run value has to stay test even if its label changes. Cancel is added automatically and starts nothing. See Ask for confirmation for every field.
Verify the whole path
Sign in as a member of the action’s user group without any workflow permission, and open the test letter batch.
Under Workflow Instances, each run’s Owner is the person who clicked, and its variables hold
choice and the script’s result in undo. The history shows each Workflow.SetMessage with the message it set.
When it does not work
The script deletes letters as the Script Engine’s service account, whatever rights the person who clicked has on Letter. The action’s user groups decide who can withdraw a batch, so keep them as narrow as the task requires.
Where to read next
Workflow trigger or script hook
Which extension point to reach for when a content event should cause something, and what changes if you pick the other one.
Client defaults and document actions
Who can configure and run a workflow action, every confirmation field, and what the person sees.
Action reference
Workflow.SetMessage and background Script.Run, with their parameters and rules.