Calculation Rules

   MaestroThe UI design product.  |   Form Builder |  All versions This feature is related to all versions.

Maestro allows you to create various Calculations rules, which you can add to components and fields of a Maestro form. The calculation rules are part of the business rules and currently consist of the Script rule.

Calculation rules must return a value. If you have any looping or branching logic, then you must include a return statement at the end of the script, for example, return value;. If you just want to return a simple expression, for example, 'data.postCode + " additional text"', then you can leave out the return and Maestro will add it automatically.

Calculation rules aren't explicitly initiated by Maestro, they're initiated through AngularJS whenever the data model changes.

Maestro executes Calculation rules to a form in the following order when:

  1. As soon as AngularJS is given some form XML data to create the data model, Calculation rules are executed, which means they run before the Form Load rule.
    Note

    This is only applicable to Calculation rules that belong to items that are visible when the form loads, that is, only either the first page, or a landing page. if there is one.

  2. An item, such as a field, has become visible.
  3. An item's value, that a rule depends on, changes.

It is important to understand the order of rules execution so empty values are not passed to calculation rules, which will break the rule's logic or cause an exception.

You can use the Script rule in several distinct ways:

Mathematical Calculation

Mathematical Calculations allow you to create simple and complex mathematical calculations (+, -, *, etc.). Calculations can be created using data entered in components on the form, or by using defined numerical values. Mathematical calculation rules can be created in repeating blocks.

Duplicate Data

Duplicate Data from Another Component allows you to populate components in the form using data entered in another components.

Preset Data

You can preset some field's values as defaults on form load using a calculation rule. For example, you can select a default checkbox option when the form loads by setting its value to true, if the value is blank, using the following JavaScript:

if (Util.isBlank(data.checkBox)) {
    data.checkBox = true
}

It's important to note that we check whether the value is blank or not before setting its value, because if you open the form again after saving it when you have unselected the checkbox, the rule will set the default value again, as well as that of on a receipt.

Note

You can also set the value to true by using the Form Load rule, the Integration pane, or in the initial XML file, which Manager uses to prefill a form. The later is the easiest and most reliable option.

Concatenate Multiple Components

Concatenate Multiple Components allows you to take data entered into multiple components and combined the entered data into one component. An example of this is asking for a First Name and Last Name and combining them into a single Full Name component.

Next, learn how to create a mathematical calculation rule.