How to: Reference a Global Variable in a Component
Overview
Let's explore how to reference a global variable Global variables are variables you can create and reference across all applications in an environment stage. in a component. Global variables help you avoid hardcoding reference values in your application's environment stages. They also help you avoid issues during your application's promotion. Global Variables Administration lets you standardize the way you create and reference custom variables. A custom variable is an identifier (or key) paired to a stored value. Once paired, you can reference the key anywhere in your application. This separation of the key from its value lets you use your key independent of the data it represents.
You can reference variables in all components that let you reference submission data objects. This includes data source URLs, and Calculator and Decisions components. To reference a global variable, you need its key and the prefix vars. So, the formatting is vars.Key. Alternatively, you can use vars getter in combination with a GET formula.
In this how-to guide, you'll create a variable to reference your current environment stage using Global Variables Administration. After creating your global variable, you can reference it using a Calculator component. It's important to note that the global variable you create is only available in the environment stage where you created it. To have your global variable resolve in another environment stage, you must promote it. Or, create it manually in another environment stage. That way, your values can resolve dynamically based on the environment stage your application is running in.
Don't use Global Variables Administration to store sensitive data, like APIs and secrets. Global variable values display to users while building applications in Unqork.
To learn more about promoting global variables, visit our Global Variables Administration: Global Variables Promotion article.
Here’s how your completed module looks in the Module Builder:
What You'll Learn
In this
What You Need
To add and reference a global variable, you need:
- 1 Global Variable
- 1 Initializer component
- 1 Calculator component
- 1 Hidden component
These instructions assume you have a module open, saved, and with a title.
Preconfiguration
Add a Global Variable
First, add a global variable for your current environment's URL. This how-to guide focuses on creating a global variable for your Staging environment.
1. | At the top right of the Unqork Designer Platform, click the Settings ▾ drop-down. |
2. | Click Administration. |
3. | Under Environment, select Global Variables. |
4. | At the bottom of the Global Variables page, click New Global Variable. |
5. | In the Enter Key field, enter envUrl. |
6. | In the Enter Description field, enter Staging URL. |
7. | Clear the Server-Side Only (Unchecked) checkbox. |
8. | In the Enter Value field, enter your Staging environment's URL. The formatting is: {your-environment}-staging.unqork.io. |
9. | In the Actions column, click Save. |
Configuration
Configure the Initializer Component
Now that you have your global variable, configure a module to reference your global variable.
1. | In the Module Builder, drag and drop an Initializer component onto your canvas. |
4. | In the Outputs table, enter the following: |
Property ID | Type | Value |
---|---|---|
calcEnvUrl |
trigger |
GO |
This Property ID corresponds to the Calculator component you'll set up next.
5. | Click Save & Close. |
Configure the Calculator Component
Next, configure a Calculator component to reference the global variable. To reference your global variable, you must input it as vars.Key. In the case of this how-to guide, the input is vars.envUrl. Then, output the value into a Hidden component that you set up next.
1. | Drag and drop a Calculator component onto your canvas, placing it below your Initializer component. |
3. | From the component's configuration window, click the Actionstab. |
4. | In the Inputs table, enter the following: |
Property ID | Alias | Required |
---|---|---|
vars.envUrl |
E |
(checked) |
5. | In the Outputs table, enter the following: |
Property ID | Formula |
---|---|
envUrl |
=E |
6. | Click Save & Close. |
Alternatively, you can input vars as the Property ID and use the output formula: =GET(E, 'envUrl'). This formatting works for any global variable key: =GET(E,'Key').
Configure the Hidden Component
Lastly, set up a Hidden component to store the value of your global variable.
1. | Drag and drop a Hidden component onto your canvas, placing it below your Calculator component. |
3. | Set the Store Data in Database toggle to (ON). |
4. | Click Save & Close. |
5. | Save your module. |
You must be an Administrator to create global variables, but you can still view the environment stage’s variables in the DevTools Console if Server-Side Only is deselected. If your Administrator selected Server-Side Only, use the Server Side Execution Testing page to view the global variable. Knowing where to look for your global variables will help if any issues arise.
Here’s your global variable in the DevTools Console:
Here’s your global variable in the Remote Execute Debugger of the Server Side Execution Testing page:
Alternatively, you can also use global variables directly in a Calculator and Plug-In component using the following syntax: vars.globalVarName.
Resources