Description
Variables in Studio Pro can have two scopes - local and global. The global scope means that the variable will be accessible (you will be able to access and use it in any activity) from any .neek file running in Studio Pro. The local scope of a variable means that a variable created in a certain file can only be accessed within that file.
Parameters
Working with global variables is simple: no matter in which file you specify the name of the global variable, the last specified value of that variable will be substituted automatically. For example, if you set a global variable x = 100
in file a.neek, and create variable y = x
in file b.neek, then y will take the value 100
(assuming that x
has not changed its value before).
To make the desired variable global, select the "Is Global" option in the "Assign value to variable" activity parameters.
If the variable is global, the corresponding sign will be displayed next to the name of this variable in the "Variables" section.
Local variables
The use of local variables imposes natural restrictions on their availability from other files. Similar to the example above, if you set variable x = 100
in file a.neek and set variable y = x
in file b.neek, then the variable, y
will no longer take on any value because the variable x
does not belong to the b.neek file. In that case, you have to x
with some value in b.neek and then use y = x
.
Nevertheless in RPA-process development, it is often used to decompose one bot into several linked subprograms (especially if the process is large). Here the need to pass values from one subprogram to another as well as to return values from the subprogram appears (in the case of global variables there is no such need because variables are available in any file).
The examples below show how to explicitly pass values into a subroutine and how to return values from a subroutine.
Passing data into a subprogram
If you want to pass data from subprogram a.neek to subprogram b.neek, schematically it looks like this:
The screenshots above show subprogram a.neek and subprogram b.neek in a sequence.
Let's take a closer look at this algorithm. To transfer data from program a.neek to subprogram b.neek, make sure that the last activity before the "Subprogram" activity in a.neek returns a result. These activities can be, for example, "Assign value to variable", "Read text file", "Read table", and other activities that return a value (text of file read, button name, etc.). Note that if the last activity in a.neek is an activity that does not return a result (e.g. "Delay"), no data will be transferred to the b.neek subprogram.
In the b.neek subprogram itself, you must create an activity block in which you want to use the data transferred from the main program. To do this, select the "Save the previous step result" option in the required parameter of this activity. The activity block itself must follow immediately from the "Start" block. In the picture above, in subprogram b.neek the data transferred from the external program a.neek is saved in a variable. Instead of a variable block, you can substitute the desired activity, for example, "Send email", in one of its parameters select the option "Save the previous step result".
Returning data from the subprogram
To return data from the subprogram (b.neek from the example above) to the main program (a.neek from the example above), construct the workflow so that the last activity in the subprogram returns some result. As described above, examples of such activities are "Assign value to variable", "Read text file", and similar activities that return a value. In the main program, after the "Subprogram" activity, you need to create an activity that accepts the result (see screenshot below).
Example of transferring and returning values
Let's build a simple workflow example where data is sent to and returned from a subprogram. The first step is to create a .neek file of the main program. The algorithm will look like this:
Here the variable x = 10
is created. Since the "Assign value to variable" activity returns the result, followed by the "Subprogram" activity, the value 10
will be passed to the subprogram.
The subprogram file b.neek will look like this:
Here the first activity in the algorithm is to save the passed value into a variable. Thus, the variable y should get the value 10
.
The last activity in the subprogram is to declare a new variable z = 20
. Since this is the last activity and it returns the result, the value 20
will be passed back to the program a.neek. As you can see from the first screenshot in this example, after the "Subprogram" activity in a.neek, the result of the subprogram is stored in the variable p
, which means it should equal 20
.