Parsware
All articles

Process design

Code on a task

Every step in a process can run code at the moments that matter to it: when the run arrives, when the task is given to someone, when they finish it, when the run leaves. A user task also works out who gets it and which record they look at. We opened the manager's step in the Leave Approval sample and went through all six.

The code page for Review Request, On complete, with the list of the step’s six code slots down its side, next to the title "Code on a task"

A step in a process is more than a box. It can run code at the moments that matter to it, and a user task also works out two answers: who gets it, and which record they look at. All of it is BS, and all of it is written in the same place.

We used the Leave Approval sample from samples/solutions/bpms-leave-approval-process. Open its process and click Review Request, the manager's step.

The step's code list

At the bottom of its properties is Code:

Review Request selected; at the bottom of its properties, the Code list: On enter, On assign, On complete, On exit, Assigned to and Record, with On complete, Assigned to and Record marked Written

Each row says Written or Nothing written, so you can see at a glance where a step does something. Review Request has three rows written.

(While we were here we fixed the Due within box just above it. Its placeholder, No deadline, was cut off to "No d" because the box was too narrow.)

The moments a step reacts to

The first four rows are events, moments in the step's life:

Slot When it runs
On enter The run reaches this step.
On assign The task has been given to somebody. User tasks only.
On complete That person has finished it. User tasks only.
On exit The run leaves this step, whichever way it goes.

On assign and On complete only exist on a User task, because only a user task has a person in it (task types explains the rest). Every other type has just On enter and On exit. Change a user task into a script task and those two disappear, along with anything written in them.

The process itself has three moments of its own, on its properties: On start, On complete and On cancel. Leave Approval uses On start to give approved its first value, false.

The code page

Select a row and the code page takes over the middle of the designer:

The code page for Review Request, On complete: two lines that read the leave request and set approved; the slot list down its left side with On complete highlighted

It's the full-width BS editor, with checking, completion and signature help as you type. Down its left side is the same list of slots, so moving from On complete to On enter is one click. The diagram comes back when you close the page with the ×.

Review Request's On complete is:

var request = getRecord("par_leaverequest", getRecordRef("record"));
setVariable("approved", request.par_decision == "Approved");

When the manager finishes the task, it reads the leave request this run is about, and records whether they chose Approved in the variable approved. The branch after it only reads that variable.

There's no Save on the code page. Save in the title bar saves the whole process, code included.

What each slot can call

The eye in the header lists what you can call from the slot you're in. Here it is on On complete, filtered to Variable:

What a script can call, filtered to Variable: getVariable("approved") under Variables, and getVariable and setVariable under Functions, with Runs when that person marks the task done at the bottom

A step's events can change things, so setVariable is there, as are functions that update records. A branch condition only gets the reading half, which is why setVariable is refused in one.

What a step works out

The last two rows aren't events. They're answers the platform asks the step for:

  • Assigned to: who should do the task.
  • Record: which record they look at while they decide. Usually getRecordRef("record"), the record this run is about.

A user task with parameters gets one more row per parameter, named after it.

Open Assigned to and the eye lists different things again:

The code page on Assigned to: role("par_manager"); What a script can call, filtered to role, shows role(name) Whoever holds a security role, with the note Who this task goes to. Answers a person, a role or a team.

Only Assigned to is offered role(), user() and team(), because only it answers who. Here it is role("par_manager"): whoever holds the manager role. A step with no Assigned to falls back to its lane's role, so you only write one where a step differs from the rest of its lane. Assignment has an article of its own next.

In the process document

A step's code is a scripts object on its node, one entry per slot that has something written, plus its assignment:

{
  "id": "node-review",
  "kind": "task",
  "taskType": "user",
  "laneId": "lane-manager",
  "title": "Review Request",
  "assignment": "role(\"par_manager\")",
  "context": { "kind": "form", "reference": "par_leaverequest", "record": "getRecordRef(\"record\")", "mode": "edit" },
  "scripts": {
    "onComplete": "var request = getRecord(\"par_leaverequest\", getRecordRef(\"record\"));\nsetVariable(\"approved\", request.par_decision == \"Approved\");"
  }
}

The slots that say Nothing written simply aren't in it. The process's own moments are in its meta.scripts, as onStart, onComplete and onCancel.

What a process script can use

The eye lists everything for the slot you are in. Grouped, the process-specific part is:

Group Functions For
Process processName, processTitle Which process this is
Run instanceId, instanceStartedBy, instanceStartedAt, instanceOrgUnit This run: who started it, when, for which unit
Route tokenId, tokenNode Which path through the process this code is on
Variables getVariable, setVariable Values the run carries from step to step
Records getRecordRef, getRecord, updateRecord, createRecord, … The record the run is about, and others
Dates addDays, minutesBetween Working out deadlines and durations
Stopping fail Ending the run with a message, when it can't go on

A branch condition gets only the reading half of this list; a step's events get all of it.

A slot for each kind of work

The four events suit different jobs, and putting each job in the right slot keeps a process predictable:

Slot A good job for it Example
On enter Mark the record as being at this stage updateRecord(…, { par_status: "With manager" })
On assign Note that somebody has it A picked up date on the record
On complete Turn the person's answer into the run's data Read the decision, setVariable("approved", …)
On exit Tidy up, whichever way the run leaves Clear a temporary flag

And the process's own:

Slot A good job for it
On start Give every variable a first value; mark the record in progress
On complete Write the final outcome, once, when the run ends
On cancel Put the record back, when a run is stopped before it finishes

The leave sample's On start is one line, setVariable("approved", false);, so the branch is never asked about a variable that doesn't exist yet. Every process with a branch should have one like it.

A script that refuses to go on

Sometimes a step finds the run can't sensibly continue: the record it is about has been deleted, or a value it needs isn't there. fail ends the run with a message, which appears in the run's history, rather than letting it limp on to a step that would do the wrong thing:

var request = getRecord("par_leaverequest", getRecordRef("record"));
if (isEmpty(request)) {
  fail("The leave request this run is about no longer exists.");
}
setVariable("approved", request.par_decision == "Approved");

Watch a process run shows where a failed run's message appears, and what can be done next.

Deadlines from dates

A step's Due within sets a deadline in the designer. When the date depends on the record, such as five days before the leave starts, a step's code can work it out with the date helpers, addDays and minutesBetween, and keep it on the record, where people can see it. Deadlines and timers covers the deadlines the process itself acts on: escalating a task, or deciding it, when time runs out.

Questions that come up

Does a step's code run as the person who did the task? A run acts on behalf of the person who started it, and the run's history records each step. A record changed by the process shows the change in the audit log with the process named as the agent.

Can code in one step read what another step's person typed? Through variables. A coform can write what a person typed into a variable when they finish, and any later step can read it with getVariable.

What if the code has a mistake? The editor underlines it as you type, and publishing checks the whole process. A mistake that only shows when the run reaches the step stops that run there, with the error in its history.

Try it

Open On enter on Review Request and write

setVariable("approved", false);

The slot list now says Written next to it. That line would reset the decision every time the run reaches the step. Clear it again, or close without saving.

Next: assignment: who gets the task, and how to keep it from the person who asked.