Using variables in application development
Overview
In application development, variables are used to store dynamic values that can be used by any user anywhere within the application or on a specific page. A variable lets you define constant key-value pairs. These can be used to:
- Call out values within page components of a page.
- Filter data inside your workflows like board or process.
There are two types of variables that you can use: Global variables and local variables.
Global variables: Global variables are declared as a common value that can be used throughout the application any number of times by any user. Once declared, a global variable stays in the app throughout the duration the app is running.
Local variables: Local variables are created within specific pages and can be used only within the page where they are created, limiting their scope. Other than the scope, a local variable can be used any number of times within the page where it is defined.
Global Variable |
Local Variable |
Global variables are declared as a common value in the app. |
Local variables are declared within a specific page in the app. |
The variable’s scope is global, i.e., all forms in the app can access it.
|
The local variable’s scope is limited to the page where it is created. No other page inside the app can use a local variable created within another page. |
Any change in the global variable affects wherever it is used inside the app. |
Any change in the local variable affects the components where it is used within the page it is defined. |
A global variable is active in the application memory for the entire time the app is in use. |
A local variable is active whenever the page where it is defined is running. |
Advantages of using local variables
Local variables offer a limited scope compared to global variables. However, using local variables can help you manage memory usage, increase processing speed and improve the application's overall performance.
While it is not possible to use the same variable name for the same type of variable, you can create global and local variables by the same name to organize the app better and prevent any naming conflicts.
Local variables can ensure improved scalability by creating as many variables as needed to execute quickly and handle larger workloads.
Creating global variables
- Open an app in Kissflow.
- Go to Application builder.
- On the left navigation bar, go to Variables.
- Click Global variable and enter the details of the variable.
- Variable name: The key name for your variable.
- Description: The description for your variable.
- Variable type: You can select the following data types: Number, Text, DateTime, and Boolean.
- Default value: Specify the default value for the key. The default value differs based on the data type.
- Click Create to save the variable.
Alternatively, you can also add new global variables from the JavaScript editor within a form.
On the Variables page, all existing global variables will be listed along with their name, description, data type, and default value. You can also update or delete global variables from this section.
Creating local variables
- Open an app in Kissflow.
- Go to Application builder.
- On the left navigation bar, click Pages and open the required page.
- Under Page properties, click Local variable and enter the details of a variable.
- Variable name: The key name for your variable.
- Description: The description for your variable.
- Variable type: You can select the following data types: Number, Text, DateTime, and Boolean.
- Default value: Specify the default value for the key. The default value differs based on the data type.
- Click Add to save the variable.
All the local variables created within the current page will be listed here. You can also update or delete a variable from this section.
Alternatively, you can also add new local variables from the JavaScript editor within a page.
Naming conventions for variable names:
- Global and local variable names cannot be empty or start with numbers or any other special character, such as an underscore.
- A variable cannot have the same name as another variable of the same type.
- Reserved keywords cannot be used as variable names. Click here for a list of reserved keywords in Kissflow.
How to declare global and local variables in the javascript editor?
Use the following syntax to declare new global and local variables within the JavaScript editor in a form.
Global variable
let accountId = idResp._id;
kf.app.setVariable(“Account_Id”, accountId);
In which the syntax, kf.app.setVariable creates and defines a new global variable.
Local variable
let user = kf.user
kf.app.page.setVariable(“Username”, user.Name);
In which the syntax, kf.app.page.setVariable creates and defines a new local variable.
Click here to learn more about how to declare and use variables using javascript.
Note: When you delete a page from the app, all local variables that are defined within the page will also be deleted.
Where are variables used?
When you add a page component like a button or label, you can use the app variables inside the fields as dynamic values or use them inside JavaScript functions. Once the application is deployed, these global variables will render during an app session.
For example, an IT admin might want to keep track of the total number of IT assets available in an organization to manage existing assets effectively. To do this, they can store the count in a global variable called 'assets_master_count'. This variable can then be accessed and used anywhere within the application.
To retrieve the asset count for a particular department alone, the admin can create an individual page for the department and use local variables for this purpose.
FAQs
- Can I create a global and local variable with the same name?
Yes, it is possible to use the same variable name for global and local variables. They operate independently as their scopes within the application differ.
- Can a variable name be updated after creation?
No, it is not possible to update the variable name once created. However, you can edit the variable description, data type, and default value at any time.