Parsware
All articles

Process design

Branch conditions

A branch doesn't decide anything by itself. Each connection leaving it carries a condition, one line of BS that is true or false, and the run takes the one that's true. We read the conditions on the Leave Approval sample, tried to break them three ways, and fixed two things on the way.

The Leave Approval process with the yes connection leaving the Approved? branch selected, and its Condition in the properties panel, next to the title "Branch conditions"

A Branch is the diamond in a process: one way in, several ways out. The branch itself doesn't decide which way. Each connection leaving it has a condition, one expression in BS that answers true or false, and the run takes the connection whose condition is true.

We used the Leave Approval sample from samples/solutions/bpms-leave-approval-process. Open its process, Leave Approval.

The branch

Click Approved?:

The Leave Approval process with the Approved? branch selected; its Code list has On enter and On exit, both with nothing written

Two connections leave it: yes to Deduct Balance, and no to Tell Employee: Rejected. Its Code list has only On enter and On exit, and neither has anything in it. Nothing on the branch picks a way out. That's deliberate: the decision is on the connections, where the diagram shows it. A branch decided by code you can't see would be a diagram that doesn't say what the process does.

The condition on a connection

Click the yes label to select that connection:

The yes connection selected, drawn in blue; its properties are a title, a description, two colours and, under Code, Condition marked Written

Its Code list has one row, Condition, with the line Taken when this is true. One expression — it decides the route and must not change anything. Select it and the condition opens in the full-width code page:

The code page for yes, Condition: getVariable("approved"), with the properties panel still showing the yes connection

The condition on yes is

getVariable("approved")

and the condition on no is its opposite:

!getVariable("approved")

approved is a variable of the process, a yes/no value the run carries from step to step. The manager's step, Review Request, sets it when they finish. The branch only reads it. (How a step sets a variable is in the next article.)

The properties panel now stays on the connection while its condition is open. Until we tried this, opening the condition switched the panel to the whole process, and closing the code page left you there instead of on the connection you were writing.

Conditions only appear on connections that leave a branch. A task has one way out, so a condition there would be a question with only one answer. (A user task with several outcomes, like Approve and Reject, sends each one down its own connection with Taken when. That's covered in a later article.)

What a condition can call

The eye at the top of the code page lists what this code can call:

What a script can call, for a condition: the process's name and title, the run's id and who started it, the route, and getVariable("approved") under Variables

It lists things to read: about the process, about this run, and the variables. There's nothing that changes anything.

A condition can only read

The engine may ask a condition more than once, so a condition that changed something would make the process depend on how often it happened to be asked. So the editor doesn't let a condition change anything. Here's what happens if you try to set a variable in one:

The condition replaced with setVariable("approved", true), underlined in red, and the problem list under the editor: 1:1 Function setVariable not found. E030

setVariable doesn't exist here, and the editor says so as you type. Write to a variable in a step's code instead.

Mistakes the problems panel finds

We tried two more mistakes. First, a misspelt variable, getVariable("aproved"). The editor can't know which variables you meant, but the problems panel at the bottom of the canvas checks every name against the process's variables:

The problems panel open with one problem: yes — condition reads a variable called "aproved", which this process does not declare

That message used to start yes reads a variable…, the connection's bare label opening the sentence. A step's code was already named as step — which code, so a condition is now yes — condition.

Second, we emptied the condition. The panel said:

The connection "yes" leaves a Branch with no condition, so nothing decides whether the flow takes it.

Every connection leaving a branch needs a condition, and a branch needs at least two ways out.

The branch, as the process stores it

The conditions live on the edges, which is where the drawing shows them:

{ "id": "node-decide", "kind": "branch", "laneId": "lane-manager", "title": "Approved?" }
"edges": [
  { "source": "node-decide", "target": "node-balance", "condition": "getVariable(\"approved\")", "label": "yes" },
  { "source": "node-decide", "target": "node-notify-rejected", "condition": "!getVariable(\"approved\")", "label": "no" }
]

And the variable they read is declared with the process, seeded at the start, and set by the manager's step:

"variables": [{ "name": "approved", "type": "boolean",
  "description": "What Review Request decided. Seeded false on start, so the branch is never asked before there is an answer." }]
// Review Request, On complete
var request = getRecord("par_leaverequest", getRecordRef("record"));
setVariable("approved", request.par_decision == "Approved");

Read top to bottom, that is the whole decision: the manager writes a decision on the record, the step turns it into a yes or a no, and the branch's two edges each ask one side of it.

Many ways out, worked out once

A branch with several ways out is where On enter earns its keep. Suppose a request goes to one of three approvers by amount. Work out the band once, as the run arrives:

// Approval band, On enter
var request = getRecord("par_purchaserequest", getRecordRef("record"));
var band = "small";
if (request.par_amount > 1000) { band = "medium"; }
if (request.par_amount > 10000) { band = "large"; }
setVariable("band", band);

and let each way out ask one simple question:

Way out Condition
small → Team lead getVariable("band") == "small"
medium → Budget holder getVariable("band") == "medium"
large → Finance director getVariable("band") == "large"

The limits are written once, in one place, and the drawing's labels say exactly what each line means. When the limits belong to the business rather than to the process, keep them in a table and read them there, as Approval by amount does, so changing a limit doesn't mean publishing a new version. (The table and fields here are to show the shape.)

Conditions that never overlap

A run takes the connection whose condition is true, so make sure that is always exactly one:

  • Write the opposites out in full. yes is getVariable("approved"), no is !getVariable("approved"). Between them they cover every run.
  • Compare against one variable for several ways out, as the bands above do, so two conditions can't both be true.
  • Make the boundaries meet. > 1000 and > 10000 leave no gap and no overlap; > 1000 on one line and >= 1000 on another would send exactly 1,000 two ways.
  • Seed every variable a branch reads, in the process's On start, so a branch is never asked about a variable nothing has set yet. The leave sample seeds approved to false.

The problems panel checks that every way out has a condition and that every variable named exists. Whether the conditions cover every case, and never more than one, is the part you check by reading them next to the labels.

Reading the record, or a variable?

A condition can read a record through a variable a step set, or a step can read the record and set a variable for the condition. The second is almost always better:

  • The step's code can be as long as it needs; the condition stays one short comparison anybody can read.
  • The variable records the decision as it was at that moment. If somebody edits the record later, the run's history still shows why it went the way it did.
  • Watch a process run shows the variables of a run, so a decision you can see as a variable is a decision you can check.

Questions that come up

Can a condition call a function of a solution script? A condition can only read, so keep it to reading variables and comparing. Do any work, shared or not, in a step's code before the branch, and leave the answer in a variable.

What if no condition is true? Then there is no way to go, and the run can't move on. That is the case Conditions that never overlap is there to prevent; a pair of exact opposites, or a set of bands with no gaps, always leaves exactly one.

Can a branch decide on a person's answer? It can, through a variable the person's step set. But when a person is choosing, coform buttons with their own connections usually read better than a variable and a branch.

Doing the sums once

A branch with many ways out, say six bands of an amount, would need the same arithmetic in six conditions. Put it in the branch's On enter instead. It runs as the run arrives, before any condition is asked, so it can work the answer out once and put it in a variable. Then each condition only compares. On exit runs after a way out has been chosen.

The sample, redrawn

While writing this we noticed the sample's own drawing was misleading. Tell Employee: Rejected sat just to the left of Tell Employee: Approved, and the line from Rejected to the end passed straight through Approved. It looked like "rejected, then told it was approved". We moved the two steps apart in the sample, so each now has its own line to the end. The previous article explains why a curved line can do that.

We didn't save any of the experiments above. Closing the designer without saving leaves the sample as it was.

Try it

Swap the two conditions, so yes reads !getVariable("approved"). The problems panel stays quiet: both connections still have a condition. It can tell you a condition is missing or names a variable that doesn't exist, but not that it's the wrong way round. Read the conditions next to the labels on the diagram. Then close without saving.

Next: code on a task, and the moments a step can react to.