Jump to content

Global constants

From Resco's Wiki

Woodford administrators can define project-specific constants. These can then be used in the business logic throughout the app project. Global constants require release 17.2.

Why global constants?

If you are an OEM/ISV partner managing complex projects with multiple variations, defining and using global constants across the entire project can save significant time and resources. In turn, this leads to faster deployment and more reliable outcomes. Allowing global constants to be defined once and accessed across all UI components (views, forms, select rules) significantly reduces the manual effort needed to modify multiple parts of the project when creating variations of a master project.

How to define global constants?

To define global constants:

  1. Edit an app project and select Settings > Configuration from the Project menu.
  2. Click Global Constants.
  3. Define constants in the same way as you would define a variable in rules editor.
    • Click Variable.
    • Select the type and enter a name.
    • Assign a value.

define global constants in woodford configuration

The following constant types are allowed:

  • Boolean
  • Date Time
  • Decimal
  • Integer
  • String

How to fill global constants from a table?

When publishing an app project, Woodford can load global constants from an arbitrary backend table and overwrite preset values. This feature is available for Dataverse, Salesforce, or Resco Cloud. It requires Woodford 19.2 or later.

Backend table configuration

Variables are loaded from a backend table if the following predefined global constant is present and non-empty:

  • resco_global_constants_connection_uri - contains a string in the following format: table://{resco_global_constants_source_table}?key={resco_global_constants_source_key_column}&value={resco_global_constants_source_value_column}. We use URI so that in the future other datasources can be used (e.g., HTTP(S)) via uri scheme, with dedicated values.

The meaning of parameters:

  • resco_global_constants_source_table - logical name of the table (you can see the logical name in the entity detail screen in Woodford)
  • resco_global_constants_source_key_column - logical name of the column where the variable name is stored. The column must be of type String (or any compatible text-based column).
  • resco_global_constants_source_value_column - logical name of the column where the variable value is stored. The column must be of type String (or any compatible text-based column).

When the connection string is invalid, the application displays an error and continues using the variables defined in Woodford.

During validation and publishing, Woodford checks if the table is properly configured, if it exists on the backend, and has declared columns. If not, it raises a validation error. When matching variable names, case-insensitive comparison is used, and literals are stripped of leading and trailing whitespace.

Data retrieval logic

Resco uses only rows of "resco_global_constants_source_table" where the value of “resco_global_constants_source_key_column” matches a user-defined global constant variable name. Case-insensitive comparison is used, and names are stripped of leading and trailing whitespaces. Any other rows in the table are ignored. If there are multiple rows with the same variable name, Resco uses the last one (using the standard "modified on" column).

Resco takes the value of "resco_global_constants_source_value_column", trims leading and trailing whitespaces, and preserves the case. Resco parses each value to fit the declared data type using Invariant Culture. If parsing fails, an error is logged, and the app displays an error message to the user. For values that are not downloaded or parsed, the value defined in Woodford is used as the fallback.

Values of global constants are cached and automatically refreshed each time the application syncs with the backend (even in case of non-blocking sync errors), unless sync is in a fatal error. For performance and data-consistency reasons, Resco uses the offline database to load values. Therefore, the table "resco_global_constants_source_table" and columns "resco_global_constants_source_key_column" and "resco_global_constants_source_value_column" must be enabled in Woodford, and the sync filter must permit all rows with values of variables. If the table or columns are not enabled, the app displays an error message to the user, and values as defined in Woodford are used as the fallback.

The fact that global constants are sourced from a table is transparent to the app maker, and their behavior remains the same regardless of the source.

For performance reasons, the maximum number of variables that can be loaded from the backend is 500. If more is needed, contact Resco support to facilitate the exception. Although a larger number of variables can be enabled, it is not covered by Resco support.

How to use global constants?

Constants can then be accessed in multiple places:

Rules
  • Available in rules configured using Woodford
  • Not available in other rules, e.g., in Report Designer or Questionnaire Designer rules

using global constants in rules

JSBridge - MobileCRM.GlobalConstants
  • Use MobileCRM.GlobalConstants.getAllAsync() to retrieve a list of constants
  • Use MobileCRM.GlobalConstants.getAsync("constantname") to retrieve a particular value
<script>
    MobileCRM.GlobalConstants.getAsync("stringEnv").then(o => {
        alert("stringEnv: " + o);
    });
    MobileCRM.GlobalConstants.getAsync("intEnv").then(o => {
        alert("intEnv: " + o);
    });
    MobileCRM.GlobalConstants.getAsync("boolEnv").then(o => {
        alert("boolEnv: " + o);
    });
    MobileCRM.GlobalConstants.getAsync("dateEnv").then(o => {
        alert("dateEnv: " + o);
    });
    MobileCRM.GlobalConstants.getAsync("decimalEnv").then(o => {
        alert("decimalEnv: " + o);
    });
        
    MobileCRM.GlobalConstants.getAllAsync().then(o => {
        let str = "";
        Object.keys(o).forEach(e => str += e + ": " + o[e] + "\n");
        alert("all: " + str);
    });
</script>

Unlike variables, constants cannot be modified, neither in the Rules editor nor in Resco Mobile CRM.