Define a scripted context

Scripted contexts make it possible to add restrictions to triggers or transitions using scripts. You define scripted contexts once, globally in your solution, and then they can be applied to any trigger or transition in your solution. You can include scripts in language conditions too, but that might cause duplication of code. Storing them centrally in a scripted context makes your solution easier to maintain.

Let's assume our solution contains flows that should only be triggered at certain times of the day, for example only before noon, when breakfast is served. We can create a scripted context called 'Is it morning?' that will return 'yes' or 'no' depending on the time of day.

Once added, the scripted context 'Is it morning?' will look as follows:

Context added to the global context manager

Create a scripted context

To create a new scripted context, proceed as follows:

  1. From the main solution window, click on the 'Solution' tab in the top left corner.
  2. In the purple section on the left, choose 'Globals' and select the 'Contexts' tab at the top.
  3. Click the 'Add' button to add a new context, a new window will open.
  4. Name the context Is it morning?
  5. Click on the back arrow in the top left of your window to go to the main scripted context window.

We can now add the script and specify the valid 'states' of our context.

Add the script

While still in the main scripted context window, paste the following script into the scripting area to the left:

// get the hour of the day
def hour = java.time.LocalDateTime.now().getHour()
// is it morning?
if (hour > 5 && hour < 12) {
    return true
} else {
    return false
}

The scripts gets the current time and if the current hour is between 5 and 12 in the morning, it will return true. On other hours, it will return false.

Scripted context added

Add Expected States

Since the script can return two values (true and false) we will need to add 'Expected states' for both them. These will be the values that can be selected when adding a context restriction to a trigger or a transition.

Add the 'Yes' state

  1. In the panel 'Expected States' on the right, replace Expected state 1 with Yes.
  2. In the field below type true - This means this expected state 'Yes' will be returned when the script returns true.

Add the 'No' state

  1. Click the 'Add' button in the 'Expected States' panel to add the seconds state.
  2. Replace Expected state 1 with No.
  3. In the field below type false - This means this expected state 'No' will be returned when the script returns false.
  4. Hit 'Save'.

Usage in flows

That's it! We now have a global scripted context that can be used to restrict an trigger or transition based on the time of day. Like this one: Transition restricted with context

You can add context restrictions in the 'Examples' panel of a trigger or transition, as illustrated here.

Was this page helpful?