Flows for APEX: Setting Process Variables from a PL/SQL Script Task

Richard AllenRichard Allen
3 min read

Several BPMN task types allow you to define the task using PL/SQL - these include BPMN Service Tasks, Script Tasks, Business Rule Tasks, and Send Tasks. This raises the question - ‘How do you set process variables from inside your PL/SQL script?’

Inside your script PL/SQL, you can set process variables using the process variable PL/SQL api (documented at https://flowsforapex.org/latest/procvar-api/#procedure-set_var ).

How do you get the current process_id and other current state variables that you need as parameters for set_var? There are two ways to access the key items of process state, like process_id within your code - you can reference them as package variables in the flow_globals package, or you can bind them (and any other process variable) using bind syntax. Let’s take a look at how we do that.

  1. Using flow_globals package variables.

The key process state variables, such as process_id are available to use in your PL/SQL script as package variables in the flow_globals package. You reference these in your script as, for example, flow_globals.process_id.

These are the items that are available as flow_globals. Some of these are only relevant in more advanced use, but I’ve included a full list for completeness — so don’t worry if you don’t understand what they all represent!

itemdescriptiontype definition
process_idthe process instance id. Uniquely identifies a running process.flow_processes.prcs_id%type
subflow_idthe path within the running process instance.flow_subflows.sbfl_id%type
step_keyessentially the step id within the running process instance.flow_subflows.sbfl_step_key%type
scopevariable scope - used to differentiate between instances of a variable with the same name in the same process instance (which only occurs when Call Activities or Iterated Tasks and sub processes are being used. Otherwise scope is ‘0’flow_subflows.sbfl_scope%type
rest_callIs this process step being called from the REST interface?boolean
loop_counteronly relevant for iterated tasks and sub-processes, to keep track of which copy of the task / sub-process is being run.flow_subflows.sbfl_loop_counter%type

So you can set process variables in your PL/SQL Script by doing something like this...

declare
    my_content_string varchar2(20) := 'Some Text';
begin
    flow_process_vars.set_var ( pi_prcs_id   => flow_globals.process_id,
                                pi_var_name  => 'My_Process_Variable',
                                pi_scope     => flow_globals.scope,
                                pi_vc2_value => my_content_string);
end;

So here is a mini process definition to show this...

and when that runs, you can see that the process variable My_Process_Variable is correctly created and set to Some Text ...

  1. Bind Syntax

    Alternatively, you can bind the key process state variables into your code. In this case, we prefix the variable name with :F4A$ when we reference it. Then you turn on binding with the ‘Allow Binding’ switch, and then select Bind Process Variables on the selector.

    Any other process variables that have been defined in the process instance can also be bound using this syntax. For example, I could reference a process variable empno into my script task as :F4A$empno'.

    Our code to set a process variable, now references the process_id and scope using bind syntax like this…

declare
    my_content_string_2  varchar2(20) := 'Some More Text';
begin
    flow_process_vars.set_var ( pi_prcs_id   => :F4A$process_id,
                                pi_var_name  => 'My_Process_Variable_2',
                                pi_scope     => :F4A$scope,
                                pi_vc2_value => my_content_string_2
                              );
end;

When I add this into my process diagram, I turn on ‘Allow Binding’ and select ‘Bind Process Variables’ in the Flow Modeler like this…

And here is the result after an instance of the process runs…. Our process variable My_Process_Variable_2 was correctly set to Some More Text.

Summary

Flows for APEX contains a process variable system, allowing processes variables to be set from within user’s PL/SQL code using the process variable PL/SQL API. Where you need to supply process state, like the process_id, this is easily available through flow_globals or as bind parameters.

For more information on Flows for APEX, see the product documentation at flowsforapex.org.

1
Subscribe to my newsletter

Read articles from Richard Allen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Richard Allen
Richard Allen

Richard became at Oracle user back in 1984 with Oracle V4.1, then joined Oracle as a Consultant working on defence and health projects. Moving to California just in time for the 1989 Loma Prieta earthquake, he was the Product Manager for database security and then Director of the Secure Systems team, and moved to Singapore to do public sector marketing. After leaving Oracle in 1997 and attending London Business School as a Sloan Fellow, he then spent ~20 years on various startup activities in Asia - helping to start 2 low-cost airlines and co-founding regional retail distribution businesses for Crocs and Totes. In 2018, back in the UK, Richard picked up Oracle again, and specifically Oracle APEX. Needing some workflow components for a project and finding that there was nothing available off-the-shelf, Richard started contributing to the Flows for APEX project when it was first announced in April 2020. Since then, he has been the main architect and developer of the Flows for APEX engine. In 2022-23, he worked on Flows for APEX and APEX Workflow as a member of the APEX Development Team at Oracle.In 2024, he has developed and launched the Flows for APEX Enterprise Edition as a paid option for Flows customers needing support, and as a way to make Flows for APEX a sustainable and well supported BPMN-based workflow solution for APEX. Richard currently slits his time between London and Singapore.