Teneo Developers

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:

i-finished context

Create a scripted context

To create a new scripted context, proceed as follows:

  1. On the Solution dashboard, navigate to the Globals tile.
  2. Click on the Add button next to Scripted Contexts.
  3. Name the context Is it morning?
  4. Click on Create.

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:

groovy

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

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.

2-is it morning

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 Expected States panel on the right, you will see an expected state has been added already. Set the name of this state to 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 on the Add button at the top of the panel to add a second state.
  2. Name this state 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. This is what the context might look like within a flow:

2-usage in flows

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