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:
To create a new scripted context, proceed as follows:
Is it morning?
We can now add the script and specify the valid 'states' of our context.
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.
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.
Yes
.true
- This means this expected state 'Yes' will be returned when the script returns true.No
.false
- This means this expected state 'No' will be returned when the script returns false.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:
You can add context restrictions in the 'Examples' panel of a trigger or transition, as illustrated here.
Was this page helpful?