Jump to content

On Change: Difference between revisions

From Resco's Wiki
Marek Rodak (talk | contribs)
Marek Rodak (talk | contribs)
Line 203: Line 203:
===Example: Appends text to the subject of the activity when you reschedule the activity===
===Example: Appends text to the subject of the activity when you reschedule the activity===


If the  
If the scheduled start for the activity is changed, appends (rescheduled) to the activity subject.


[[File:On change rule in schedule board.png]]
[[File:On change rule in schedule board.png]]

Revision as of 14:46, 1 December 2022

Rules are client-side scripts (no-code business logic) that are executed when a user of the mobile app interacts with the app. The On Change rules are checked when any field is modified. Rules are managed using the rules editor, usually in Woodford.
On Change rules are available for the following user interface components:

IsLoaded

IsLoaded is a variable that checks whether the form or questionnaire is fully loaded and visible on the screen, with all the values assigned. Use it to differentiate between the initial loading of a form and manual user change.

If IsLoaded Equals True
Warning Inclusion of isLoaded property is strongly recommended. Otherwise, it could negatively impact performance or even lead to a "Too many nested rule scripts" error.

ChangedItem

The ChangedItem property checks whether the change on the form occurs on the specified field.

If ChangedItem Equals address1_city
Warning Inclusion of ChangedItem property is strongly recommended. Otherwise, it could negatively impact performance or even lead to a "Too many nested rule scripts" error.

Forms

Form is a screen in the application that contains numerous fields that either hold or await the data. The default behavior of an entity form can be changed using form rules. Form rules describe sequences of steps that are executed on form-related events. On change usually serves as a field modification based on input or simple field validation.


Rule execution
  • When you modify any field.
  • When the form is loaded (a new form starts empty, then the values are changed as they are loaded from the database and displayed). This can be disabled by "IsLoaded" condition.
  • When an associated/related record is created or modified. This option must be enabled in the properties of an associated list. Switch to the Properties tab and enable Controls > Trigger OnChange event (more info).
  • Setting a value inside the On Change rule for a field placed on the form triggers the On Change rule again. This can be disabled by "ChangedItem" condition.

Example: Postal code validation

When a user enters information into a field, you may want to verify that the format is correct. In the rule below, we verify that the postal code field matches the expected format.

Note the three conditions:

  • IsLoaded Equals True – because we don’t want to check this condition when opening the form; we want to check the condition when the user makes a change on the field.
  • ChangedItem Equals PostalCode – because we want the rule to be triggered only if the Postal Code field was changed and not any other field on the form.
  • Entity.PostalCode Does Not Match Regex – because we want to check the format of the field after the user changed the value of the field.

Example: Placeholders and appearing fields

When we create a new record, a placeholder is assigned to the Main Phone field while the other two (email address, web site) are set to not visible. If the Main Phone is filled, the Email field is assigned a placeholder and set to visible (the Website field remains hidden). Then, if the Email is filled, it's assigned a placeholder and set to visible.

On Load part

In the On Load part of the rule, we prepare the form for a new record by:

  • filling the placeholder in telephone1 field, and
  • hiding the emailaddress1 and websiteurl fields.

On Change part

In the On Change part of the rule, we define how the form will act in case of change. If the telephone1 field is filled with data, we fill in the placeholder for emailaddress1 and set it to be visible.

Note Is Loaded equals true is important as we do not want the rule to be triggered by the placeholder for telephone1 which we set in the On Load. Entity isNew condition ensures that the rule is only executed in case of a new record. Entity E-mail and Web Site does not contain any data conditions prevent the repetition of the rule in case we already filled email or website and decided to change the telephone number.

The rule continues similarly for email. If the emailaddress1 field is filled in with data, we fill in the placeholder for websiteurl and the field is set to be visible.

Example: Load reference from another entity

This example is a showcase of loading references from one entity to another. Let's say "we want to have a detail on the (Account) form containing primary contact information (Contact)." First, we have to create a new detail and add primarycontactid field.

We create a shared variable called primarycontact (as we are going to store contact record inside, we have to choose shared variable to be contact entity type). Then we add a condition: if the primary contact field is changed, we assign Primary Contact record to the shared variable primarycontact.

This shared variable now contains the primary contact with all its data, which means we can add different fields of this shared variable to the account form.

Example: Load reference from another entity 2

In this example, we want the Order_priceperunit field to load reference from the Product_currentcost.

If Product of the Order changes, we create a variable var4, which loads reference from the selected Product. Now, we can assign var4_currentcost to the Order_priceperunit field.

Example: Count the number of associated contacts (for account)

We create shared variable "contactCount" (integer). If the parentcustomer for contact changes (see note), we create the variable "count" (integer), where we load all contacts related to the account through the Customer lookup field and count them. If the variable "count" contains data, it's assigned to the shared variable "contactCount". The shared variable can then be placed on desired form list.

Used fetch:

Note Trigger OnChange event option must be enabled for the related list (Contact).

Example: Simple calculating operation

If the user changes quantity or priceperunit, then we create variable var1 where we divide priceperunit by 5 (as we set the tax to 20%) and variable var2 where we multiplicate var1 by quantity. The result (var2) is then assigned to the field Tax.

Example: Clear selected fields on form

If Product in the Order is changed, specified fields on the form are cleared and set to the default state.

Example: Volume discount classified by product

In this rule, we specify the volume discounts for different products at different volumes.

If the quantity or product is changed, the rule is triggered. We classify products and then the volume levels with discounts. For Handrail, we have 4 levels of volume, and each category has a specified volume discount:

Volume Discount
1 <=100 0
2 <=500 800
3 <=1000 1500
4 >1000 3000

Optionally, you can calculate volume discount in percentage via variables:

Example: Calculation of the total price for the Order

If the user changes quantity or priceperunit, we create variable var3 where we multiply priceperunit by quantity. The result (var3) is then assigned to the field Total.

Views

On Change rules can be used with views, but only when the Editable field is set. (Editable field, list, or editable grid is a feature that allows users to edit one or more records directly from the view, without a need to go to the form of a record.)

Rule execution
  • When you modify an editable field.

Example: Clear the address fields on the view if the city is changed

Set all the fields (also those meant to be affected by the change) as edit or directedit type.

The postal code and street field are cleared if the city field is changed.

Questionnaires

Editing rules allow you to further customize the questionnaire form’s design and usage (hide or disable form fields, assign values to them, etc.).

Rule execution
  • When any value in the questionnaire is modified
  • When the questionnaire is first displayed
  • When a repeatable group is repeated
  • When a repeatable group is deleted

Example: Additional questions

If the user answers a question in a certain way, you may want to display additional questions. For example, if the answer to the question “Are you married?” is “Yes”, an additional question “Partner's name” is set to be visible.

Example: Automated date of next inspection

If the user answers a question in a certain way, another question is filled in, based on the answer. For example, you can count the next inspection date based on the periodicity of inspections.

Example: Populate the questions with data from shared variable

This example is an On Change addition to the On Load rule: Populate the questions with data from regarding lookup entity 2.

If the equipment question is changed, we assign this changed value to shared variable Asset. If the Asset variable contains all the data we want to use in questionnaire, it is assigned.

Schedule Board

In the Schedule Board, the On Change rule triggers when a task is rescheduled. The event is triggered when you:

  • drag a task to a different time/resource
  • modify the time or resource on the task properties
Note When you drag a task to a different resource and time, the rule is actually executed three times (every time with a different value in the ChangedItem variable): once for the start date, end date, and resource.

Example: Appends text to the subject of the activity when you reschedule the activity

If the scheduled start for the activity is changed, appends (rescheduled) to the activity subject.