On Save
| Rules and examples |
|---|
|
| Warning | Work in progress! We are in the process of updating the information on this page. Subject to change. |
The On Save rules are checked when you save a record. Rules are client-side scripts that are executed when a user of the mobile app interacts with the app. Rules are no-code business logic, which are managed using the rules editor, usually in Woodford.
On Save rules are available for the following user interface components:
- Forms
- Editable views
- Questionnaires
Save action
To understand On Save rules, it is important to understand how the saving of records works in Resco mobile apps. This is the order of events when saving:
- User initiate saving process.
- On Save rules are executed.
- The app checks whether the record can be saved (rules may interrupt save).
- If valid, the record is saved.
SaveAfter
SaveAfter defines an event in which a certain action is performed after saving (the form or questionnaire). This event is typically used in the case of a referential (look-up) entity. For example, we want to save a new Lead. We have On Save rule where we create a Task to remind us to contact the Lead. If something goes wrong during save, we will have Task without Lead. SaveAfter ensures that the Lead is saved first and only then the Task, as empty reference causes error.
- Available only for On Save and On Execute rules.
- In questionnaires, available for root questions (and not for question groups)
Display validation problems
There are two built-in string variables that can be used to display validation problems:
- ErrorMessage: A string variable where you can set an error message for form. In the case of On Save event, this stops the saving.
ErrorMessage Assign Website URL cannot be empty!
- WarningMessage: A string variable where you can set an error message for form. In the case of On Save event, this does not prevent saving, user can decide to continue.
WarningMessage Assign Website URL is empty! Continue anyway?
Forms
Form is a screen in the application that contains numerous fields which either hold or await the data. 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 Save usually serves as a final field validation or they can automate changes in related records.
- Rule execution
- When you save a record.
Example: Main phone field validation
This rule shows a warning message if the phone format is not correct. As this is only a warning, users can ignore it and save the record anyway.
This somewhat dated blog shows how to check a related record before you can save changes. Blog
Example: Validating field with style
If the Main Phone filed does not contain any data or does not match regex, assign red style to the filed. Otherwise, assign Table as style.
Example: Automatically schedule a Task while saving new Lead
If the Lead is new, we create a new task via NewInstance. Then we use steps to fill in necessary information to newTask. At the end, we add a step to save the Task after the Lead (since the task can only refer to an already existing record).
Views
On Save 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.)
Questionnaires
In inspections, there are two options when to execute On Save rules.
- Rule execution
- When you complete the questionnaire.
- When you save or complete the questionnaire.
This behavior is configured on the questionnaire level, in the Validate On property.
Example: Create a new task based on next inspection
If the next inspection date question is filled in, a new task with loaded data from questionnaire is created.
Example: Measure the duration of inspection
This rule measures the time duration of inspection. Since the rule has to contain start and end of the inspection, we need to include both On Load and On Save.
- On Load part
We create a shared variable StartInspection, where we assign current time (by SetNow). We then assign this value to start-of-inspection question.
- On Save part
We create a shared variable EndInspection, where we assign current time (by SetNow). We then assign this valuse to end-of-inspection question. We then create a variable var2 where we calculate timedifference between StartInspection and EndInspection (the result is set to be in minutes). In the end, we assign var2 to question inscpection.time.
| Note | Questions start-of-inspection and end-of-inspection are optional. They are not required for rule to work. |
Example: Check all instances of a repeatable group
In the following example, we're using the questionnaire variable QuestionGroups to access group data. Notably, it uses QuestionGroups.{group name}.All to access all instances of a repeatable group. In the questionnaire "Assets Inspection", one repeatable group is used for each asset. Within each instance of this group, the yes/no question "issue-encountered" holds information on whether the asset is okay. This way, we can use one simple condition to check the status of all instances of the repeatable group.