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

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.
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!
| item | description | type definition |
| process_id | the process instance id. Uniquely identifies a running process. | flow_processes.prcs_id%type |
| subflow_id | the path within the running process instance. | flow_subflows.sbfl_id%type |
| step_key | essentially the step id within the running process instance. | flow_subflows.sbfl_step_key%type |
| scope | variable 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_call | Is this process step being called from the REST interface? | boolean |
| loop_counter | only 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 ...

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 selectBind Process Variableson 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
empnointo 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.




