Globals - Code Examples

Example of Creating a New Global and Using it in a Function

image

Open the Globals window under a Logical Unit and define a new Global.

After the Global is saved, its definitions are kept in the Globals.java file under the same LU and its initial value = Y. This variable can be used by all functions under this LU. The example below shows how to check the value of the Global variable named CUSTOMER_CHECKS_ENABLED in the CRM LU, and to determine whether to perform or to skip specific business logic (validation checks):

if (UserCode.getGlobal("CUSTOMER_CHECKS_ENABLED", "CRM").equals("Y")) {
    //do something
}

Notes:

  • The getGlobal method has 2 versions:
    • getGlobal(String globalName) - returns the global value for this session. In case of conflict values between LU's will throw an exception.
    • getGlobal(String globalName, String lu) - returns LU global value for this session.
  • Invoking directly the Global by the user code returns the Global's value as defined in the Studio implementation and does not return the overridden value if exists. To get the Global's overriden value use either the getGlobal method or use the set command. For example:
if (ludb().fetch("SET CRM.CUSTOMER_CHECKS_ENABLED").firstValue().toString().equals("Y")) {
    //do something
}

Example of Using a Global in an SQL Statement in a Function

image

Open the Globals window under a Logical Unit and define a new Global.

The global can be used in an SQL prepared statement in an LU function. The syntax is: '@[global_name]@'. For Integer value, the use of apostrophe is optional.

String sql = "SELECT * From ACTIVITY WHERE CUSTOMER_ID = ? AND ACTIVITY_ID = ? AND NEW_NOTE_IND = @NEW_IND@";
ludb().fetch(sql, input1, input2).each(row->{
    yield(row.cells());
});

Example of Using a Global in a Table Population

image

Open the Globals window under a Logical Unit and define two new Globals and then create a new Table Population and add the Globals to it.

image

This example shows how a Global can be used in a Table Population. Since the SOURCE_PRODUCT_VERSION and ACTIVITY_NOTE Globals are defined in the LU’s Globals list in the Globals.java file, they are part of the LU’s scope and therefore can be used by the Table Population in the same LU.

Click to display an example of Globals under a Logical Unit in the Demo project.

Example of Using a Global in a Fabric Web Service

image

Open the Globals window under Shared Objects and define new Globals and then create a Web Service and use the Globals.

This example shows how Globals can be used within a Fabric Web Service.

Three Globals are defined under Shared Objects Globals in the SharedGlobals.java file and therefore can be used by the Fabric Web Service.

if (contrID == "" && adrID == "") {
    result = fnErrorCheck(getGlobal(MISSING_INPUT)); //Missing input
    return result;  
} else if (contrID != "" && adrID != "") {
    result = fnErrorCheck(getGlobal(TOO_MANY_INPUTS)); //Too many inputs
    return result;
}

Click to display an example of Globals under Shared Objects in the Demo project.

Example of Overriding a Global in a Function or a Web Service

image

Open the Globals window under Shared Objects and define a new Global and then create a Web Service or a Project function that will override the initial value of this Global.

The following examples show how a Global can be overridden in a cluster and per session.

Override the Global Per Cluster

The RECEIVED_ERROR Global is created with an initial value of 0 and in a function its value is overridden per cluster.

if (...) {
    ...
    fabric().execute("set_global global '*.RECEIVED_ERROR="+anotherValue+"'");
}

Set the Global Value on a Session Level

The RECEIVED_ERROR Global is created with an initial value of 0 and in a function its value is overridden on a session level. Check the value of the overridden variable in another function or a WS, for example to perform business logic.

In fnErrorCheck: override the global or a variable per session:

if (...) {
    ...
    fabric().execute("set RECEIVED_ERROR="+ anotherValue);
}

The wsGetCustomerDetails Web Service calls the fnErrorCheck function and then checks the value of this variable set by the fnErrorCheck function:

Object val;
String receivedErr = "";
Map<String,Object> result = new HashMap<>(); 
//invoke fnErrorCheck() which sets the value of RECEIVED_ERROR
result = fnErrorCheck(input); 
...
//get the value of RECEIVED_ERROR as set by fnErrorCheck() and do something...
val = db("fabric").fetch("set RECEIVED_ERROR").firstValue();
receivedErr = k2_ifNull(val.toString(),"No error");
result.put("p_error", receivedErr);

Previous

Globals - Code Examples

Example of Creating a New Global and Using it in a Function

image

Open the Globals window under a Logical Unit and define a new Global.

After the Global is saved, its definitions are kept in the Globals.java file under the same LU and its initial value = Y. This variable can be used by all functions under this LU. The example below shows how to check the value of the Global variable named CUSTOMER_CHECKS_ENABLED in the CRM LU, and to determine whether to perform or to skip specific business logic (validation checks):

if (UserCode.getGlobal("CUSTOMER_CHECKS_ENABLED", "CRM").equals("Y")) {
    //do something
}

Notes:

  • The getGlobal method has 2 versions:
    • getGlobal(String globalName) - returns the global value for this session. In case of conflict values between LU's will throw an exception.
    • getGlobal(String globalName, String lu) - returns LU global value for this session.
  • Invoking directly the Global by the user code returns the Global's value as defined in the Studio implementation and does not return the overridden value if exists. To get the Global's overriden value use either the getGlobal method or use the set command. For example:
if (ludb().fetch("SET CRM.CUSTOMER_CHECKS_ENABLED").firstValue().toString().equals("Y")) {
    //do something
}

Example of Using a Global in an SQL Statement in a Function

image

Open the Globals window under a Logical Unit and define a new Global.

The global can be used in an SQL prepared statement in an LU function. The syntax is: '@[global_name]@'. For Integer value, the use of apostrophe is optional.

String sql = "SELECT * From ACTIVITY WHERE CUSTOMER_ID = ? AND ACTIVITY_ID = ? AND NEW_NOTE_IND = @NEW_IND@";
ludb().fetch(sql, input1, input2).each(row->{
    yield(row.cells());
});

Example of Using a Global in a Table Population

image

Open the Globals window under a Logical Unit and define two new Globals and then create a new Table Population and add the Globals to it.

image

This example shows how a Global can be used in a Table Population. Since the SOURCE_PRODUCT_VERSION and ACTIVITY_NOTE Globals are defined in the LU’s Globals list in the Globals.java file, they are part of the LU’s scope and therefore can be used by the Table Population in the same LU.

Click to display an example of Globals under a Logical Unit in the Demo project.

Example of Using a Global in a Fabric Web Service

image

Open the Globals window under Shared Objects and define new Globals and then create a Web Service and use the Globals.

This example shows how Globals can be used within a Fabric Web Service.

Three Globals are defined under Shared Objects Globals in the SharedGlobals.java file and therefore can be used by the Fabric Web Service.

if (contrID == "" && adrID == "") {
    result = fnErrorCheck(getGlobal(MISSING_INPUT)); //Missing input
    return result;  
} else if (contrID != "" && adrID != "") {
    result = fnErrorCheck(getGlobal(TOO_MANY_INPUTS)); //Too many inputs
    return result;
}

Click to display an example of Globals under Shared Objects in the Demo project.

Example of Overriding a Global in a Function or a Web Service

image

Open the Globals window under Shared Objects and define a new Global and then create a Web Service or a Project function that will override the initial value of this Global.

The following examples show how a Global can be overridden in a cluster and per session.

Override the Global Per Cluster

The RECEIVED_ERROR Global is created with an initial value of 0 and in a function its value is overridden per cluster.

if (...) {
    ...
    fabric().execute("set_global global '*.RECEIVED_ERROR="+anotherValue+"'");
}

Set the Global Value on a Session Level

The RECEIVED_ERROR Global is created with an initial value of 0 and in a function its value is overridden on a session level. Check the value of the overridden variable in another function or a WS, for example to perform business logic.

In fnErrorCheck: override the global or a variable per session:

if (...) {
    ...
    fabric().execute("set RECEIVED_ERROR="+ anotherValue);
}

The wsGetCustomerDetails Web Service calls the fnErrorCheck function and then checks the value of this variable set by the fnErrorCheck function:

Object val;
String receivedErr = "";
Map<String,Object> result = new HashMap<>(); 
//invoke fnErrorCheck() which sets the value of RECEIVED_ERROR
result = fnErrorCheck(input); 
...
//get the value of RECEIVED_ERROR as set by fnErrorCheck() and do something...
val = db("fabric").fetch("set RECEIVED_ERROR").firstValue();
receivedErr = k2_ifNull(val.toString(),"No error");
result.put("p_error", receivedErr);

Previous