Early Access: The content on this website is provided for informational purposes only in connection with pre-General Availability Qlik Products.
All content is subject to change and is provided without warranty.
Skip to main content Skip to complementary content

Constrain

The Constrain statement can be used in combination with Let or Set statements for defining script variables. The Constrain statement allows you to define constraints on possible values for these variables. If a variable definition violates the constraints, the reload fails. With constraints, you can require variable values to match specific types, fall within specific numeric ranges, and match with acceptable values that you define.

Syntax:  

Constrain variablename = json

Where:

  • variablename is a script variable.

  • json is a valid JSON object specifying the constraints. Individual constraints are added within this object as key-value pairs.

Constraint types
Constraint type (key) Meaning Constraint requirements (value) Examples
"type" Constrain variable values to a specific data type.

"text" specifies a text type.

"number" specifies a numeric type.

CONSTRAIN vExampleText = {"type": "text"}
CONSTRAIN vExampleNumeric = {"type": "number"}
"maxnum" Set a maximum value for a variable. Numbers only (integer or floating point). Scientific notation is allowed.
CONSTRAIN vExample = {"maxnum": 5000}
"minnum" Set a minimum value for a variable. Numbers only (integer or floating point). Scientific notation is allowed.
CONSTRAIN vExample = {"minnum": 250}
"valuesnum" Define a list of acceptable numeric values for the variable. Comma-separated list of numbers enclosed in square brackets. For example: [1,2,3]
CONSTRAIN vExample = {"valuesnum": [1,2,3]}
"valuestext" Define a list of acceptable text values for the variable. Comma-separated list of strings enclosed in square brackets. For example: ["a","b","c"]
CONSTRAIN vExample = {"valuestext": ["Department A", "Department B", "Department C"]}

When to use a Constrain statement

Use the Constrain statement to prevent unwanted variable values from being loaded into analytics apps. The following sections outline specific ways in which you can use it.

Reload-time variable update

Constrain is especially useful when used in combination with reload-time variable updates. With reload-time variable updates, you can dynamically update variables during app reloads, using the variables property on the Reloads API. The Constrain statement prevents malicious or improperly formatted variable definitions from being incorporated into analytics reloads.

Reload-time variable updates, along with the Constrain statement, support the following use cases:

  • Templated apps that load data selectively based on conditions such as customer ID or name (which can be passed at load time as variables)

  • Central control over apps distributed across many Qlik Cloud tenants

  • Migrating variable-oriented workflows and tasks from QlikView and Qlik Sense Client-Managed into the cloud

Security, reliability, and collaboration

Even when used outside of reload-time variable update scenarios, Constrain enhances security controls for load script authoring—for example, during collaborative load script development. For instance, an app owner could define constraints for variable values, informing collaborators of specific conditions that need to be met for the app to reliably be reloaded successfully.

Considerations

  • The Constrain definition for the variable must be a valid JSON object.

  • You can specify more than one constraint for a single variable. All constraints for a variable need to be contained within a single constraint object. For examples, see Examples - Multiple constraints.

  • If you have already loaded a script variable with a certain name into the app, future attempts to define constraints and values for that variable can fail due to conflicts between the original and the redefined variables.

    To resolve these conflicts, reset the variable constraints and values by inserting blank definitions. Examples:

    CONSTRAIN x;
    LET x;
    
    CONSTRAIN x;
    SET x;

    You can then redefine the variable and its constraints in subsequent sections in the load script. For a long-form example, see Example - Resolving variable definition conflicts.

  • The order of the variable definition and the constraint definition does not matter. However, in the event of variable-related reload failure, the line at which the break point occurs will be different.

  • The Constrain script statement only restricts variable definitions for script variables. You can also apply constraints on script and end-user variables (those created or updated in sheet view or as session variables using the API) by using the public API. For more information, see Developer alternatives for defining variable constraints.

Developer alternatives for defining variable constraints

You can also use the public API to define variable constraints, rather than using the Constrain statement in the load script. When defining constraints via API, all attempts to update variable values—whether directly in the load script, in sheet view, or via the Reloads API—will be subject to the constraints.

To define variable constraints via the public API, use the GenericVariableConstraints object in the Qlik Sense Engine (qix) API: GenericVariableConstraints.

Example - type

Example - maxnum

Example - minnum

Example - valuesnum

Example - valuestext

Examples - SET versus LET

These examples show the differences between how constraints are applied when using SET or LET during variable definition. All examples use constraints applying a type requirement, but these principles apply to all constraint types generally.

Examples - Multiple constraints

Example - Resolving variable definition conflicts

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!