Creator Help

Sample Application

The application Global Variables illustrates how to define Global Variables and access them within applications or across applications, using Functions. This application stores the global variables (Global Key and Global Value). It also defines functions to enable other applications to access these global variables, update their values or add new global variables.


The application Global Variables comprises of the following Forms and Functions:

  1. Form1 with fields Global Key and Global Value to enter the global variable name and the variable value.
  2. Function getGlobalValue to access the global variable from within this application or from other applications.
  3. Function updateglobalvalue to update the global variable from other applications.

a. Function – getGlobalValue


The function getGlobalValue returns the value of a global variable based on the key specified. If the variable key does not exist in form1, null value is returned.

string sample.getGlobalValue(string variableName)
{
if (count(Form1[Global_Key == input.variableName]) >0)
{
col = Form1 [Global_Key == input.variableName];return col.Global_Value;}
else
{
return "";}
}


Code Explanation

 

string sample.getGlobalValue(string variableName) – The function getGlobalValue is defined with variableName as a string argument. The function will return a string value.


if (count(Form1[Global_Key == input.variableName]) >0) – Counts the records in Form1 whose Key is equal to the value of the argument passed to the function. The If statements are executed, if the count is greater than 0. If not, the else statement is executed.


col = Form1 [Global_Key == input.variableName];- Fetches the record inn Form1, whose Key is equal to the value of the argument passed to the function. Stores it in a collection variable named “col”


return col.Global_Value; – The function returns the Global_Value from the collection variable named “col”


return “”; – If the variable key does not exist in form1, the else statement returns null value.


b. Function – updateglobalvalue


The function getGlobalValue updates the value of a global variable, based on the key specified. If the variable key does not exist in form1, a new record is added to Form1 with the specified key and value.

void sample.updateglobalvalue(string variableKey, string variableValue)
{
if (count(Form1[Global_Key == input.variableKey]) >0)
{
col = Form1 [Global_Key == input.variableKey];
col.Global_Value = input.variableValue;
}
else
{
insert into Form1
[
Added_User = zoho.loginuser
Global_Key = input.variableKey
Global_Value = input.variableValue
]
}
}


Code Explanation:


void sample.updateglobalvalue(string variableKey, string variableValue) - The function updateglobalvalue is defined with string arguments variablekey and variableValue as string arguments. The function will not return any value.


if (count(Form1[Global_Key == input.variableKey]) >0) – Counts the records in Form1 whose Key is equal to the value of the argument (variableKey) , passed to the function. The If statements are executed, if the count is greater than 0. If not, the else statement is executed.


col = Form1 [Global_Key == input.variableKey];- Fetches the record in Form1, whose Key is equal to the value of the argument (variableKey) passed to the function. Stores it in a collection variable named “col”


col.Global_Value = input.variableValue; – Updates the col.Global_Value with the value of the argument (variableValue) passed to the function.


return col.Global_Value; – The function returns the Global_Value from the collection variable named “col”


else - If the variable key does not exist in Form1, the else statements are executed, which adds a new record in Form1 with the specified key and value.


ADDING THE FUNCTION TO YOUR APPLICATION

  1. Select the Function tab from the Script tab.
  2. Click on the New icon in the Functions tree, displayed on the left-side
  3. Select the option Write script/copy sample function
  4. Copy/Paste the function code to the text area and click Done to update the changes.
  5. The function named getGlobalValue will be created under the namespace sample.
ACCESSING THE GLOBAL VARIABLES

The application Update Global Variables illustrates how to access the “Global Variables” defined in the “Global Variable” application by calling the above functions. This application contains a Stateless form named Update Global Variables with fields Enter Key and Value.

  1. When a key is entered in the above form, the Function getGlobalValue is called to fetch the value of the key, from the “Global Variables” application and display it in the current form.
  2. The modified value is then updated in the ‘Global Variable” application by calling the updateglobalvalue function.
  3. If a new key,value pair is entered, the same is added as a new global variable .

Calling the Functions


a. The function getGlobalValue defined in the ‘GlobalVariable” application is called from the on user input script of the Enter_Key field in Update_global_Variables form, as highlighted in the code given below. The script is executed when a global variable key is entered in the Enter_key field. The function will return the global variable value from Form1 and updates it in the Value field. The Set Variable deluge statement is used to set the Value field (input.Value) with the value returned by the getGlobalValue function.


b. The function updateglobalvalue is called from the on click script of the form with the input key and value as arguments to the function. The script is executed when the form button is clicked. The function will update the value of the global variable, in the “Global Variable” application.

form Update_global_Variables
{
displayname = "Update global Variables"
store data in zc = false

must have Enter_Key
(
displayname = "Enter Key"
type = text
on user input
{
input.Value = globalvariable.sample.getGlobalValue(input.Enter_Key);
}
)

must have Value
(
type = text
)

actions
{
on_click
(
type = submit
displayname = "on click"
on click
{
globalvariable.sample.updateglobalvalue(input.Enter_Key, input.Value);
}
)
}
}

To install the application


Learn how to install the application to your account - click here.

Top