# 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](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!

| 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...

```sql
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...

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742900017392/6b293cd0-de1c-4ca7-9824-d2baf931d99b.png align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742900030275/15865406-3072-406d-afed-3579958f0817.png align="left")

2. ## 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…
    

```sql
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…

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742900656893/8856c363-f156-4feb-9a98-457a5d29e1e4.png align="center")

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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742900643270/432cd380-1f19-4546-a5aa-219c941d2862.png align="center")

## 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](https://flowsforapex.org).
