Understanding and use case of Global Variables and Constants

Global Variables and Constants

Global variables and constants are the most important feature that is newly introduced by UiPath 2022.10. They allow you to store data in a central location that can be accessed by all parts of your program. You can identify them easily by using the global keyword. For example, if you want to use the global variable Config in your Sequence/Project, you would write: global_config, If you want to use a constant, you would write: constant_(Name of the constant)

When working with data in a workflow, it can be helpful to have the ability to share data between the different activities in the workflow. This is where global variables and constants come in. Global variables and constants allow you to store data in a central location that can be accessed by all the activities in your workflow. This means that you don’t have to pass the arguments for this. There are a few things to keep in mind when working with global variables and constants. First, you need to make sure that the variable or constant is actually defined before you try to access it. If the variable or constant is not defined, you will get an error. Second, you need to be careful about how you modify global variables and constants. If two activities in the workflow try to modify the same variable or constant at the same time, you may end up with unexpected results. Overall, global variables and constants can be a helpful way to share data between different activities in a workflow.

When working with data in a workflow, it can sometimes be necessary to share that data with another workflow in the same project. With global variables and constants, this is possible without having to pass the data as an argument. This can be extremely helpful when working with large or complex data sets that would be difficult to manage if they were passed as arguments. It also allows for greater flexibility when working with data in a workflow, as you can easily access and modify global variables and constants as needed. When used properly, they can save time and simplify the process of sharing data between different workflows.

Let’s have a look at how to Create/define Global Variables and Global Constants in UiPath Studio stepwise:

Steps to create a Global Variable:

Step 1 . Open the Data Manager from the right-side panel of your UiPath Studio.

Step 2 . Look for Variables, and under variables click on New Variable. Data Manager > Variables > New Variable.

Step 3 . Then fill in all the fields as per your Project requirement.

Screenshot (250)

Step 4. To make this variable a Global Variable you need to change the variable scope from Main to Global.

Screenshot (251)

Your global variable is created successfully, pass this global variable in the activities to use the variable throughout the Project.

Screenshot (252)

Steps to create a Global Constant:

Step 1. Open the Data Manager from the right-side panel of your UiPath Studio.

Step 2. Look for Constants, and under constants click on New Constant. Data Manager > Constants > New Constant.

Step 3 . Then fill in all the fields as per your Project requirement, and click ok.

Screenshot (254)

Your global constant is created successfully, pass this global constant in the activities to use the constant throughout the Project.

Screenshot (255)

By: @sachin.lather @Anshdeep_Tyagi

9 Likes

@sachin.lather Thanks for sharing. It is really very helpful for us.

1 Like

@Anshdeep_Tyagi Thanks

The “Global” option is not available for me in the scope dropdown list:

Hello there,

we want to use Global variables in our ReFramework to avoid passing config Dictionnary via argument in all our workflows and save some precious time.
We are experiencing that by changing workflow, our config object if always null, even if we fill it correctly.

Is global variable only meant to be used with simple types and not complex ones ? We have seen same behiaviour while trying to put a UiPath.Core.Browser object in global.

Thanks for your help

EDIT: I’ve found that to assign something to complex types with global scope,
we need to write the fullname including namespace:
GlobalVariablesNamespace.GlobalVariables.config ( config is variable name )
after it, we can call just config. In case someone else has issue :slight_smile:

1 Like

Can Global Variables and Global constants replace arguments? if yes, why should one use arguments rather than the above?

Hello @hp321 ,

For me answer is yes. I’ve modified our Reframework template for my company to have config now in global variable. So I never pass it as an argument and save a lot of time.

IMO it should be now out of the box for UiPath reframework template.

Hope this helps

1 Like

I would argue that Global variables should be used carefully and not as a default. For reusability of components and subprocesses, input and output arguments clearly state what is required to input for the component to function, so are still required here. I can see the benefit of having the config file at the global level (as this should be available to all workflows across the project) but to have all variables as global is a lazy approach to development and can get very messy to manage.
The approach we are taking in our team is to justify the use of each global variable, for example to store the current state of an application, so that this information is not lost for any exceptions etc, so there is a benefit to having this globally. Otherwise we will continue to use arguments.

2 Likes

Thanks @katharine.hardy,
Just wondering, what do you mean by an application state?

The example we are using right now is for which account is currently logged in. We have a process where only the account that created a letter in the application can cancel that letter. So we are setting a global variable to indicate the username of the account logged in, so we can check if that is the correct account to cancel the letter for the current transaction, and if not, we change the account (using a proxy or log out and log in) and update the global variable so we have “current state” of the application stored there, and can continue to use it for checking the next transaction.

1 Like

Definitely agree,

config for me is perfectly fine because always loaded in all projects ( and also in Unit tests in the Given part ), but everything is not meant to be used as a global variable, which can cause complexity and hard to read workflows. Also in a testing perspective it will be a mess to test because no in argument to have a data driven approach for instance.

We currently use global variables for two things : config and browser instance for topics relatives to our ERP. As soon as in the initialization part we always open browser and log in, we will have have an opened browser. Also makes sense while using unit tests because in given part we init all applications.

Have a nice day !

3 Likes

Very very good in attending this program.