Skip to content

Create Rules

Rules can do many things: You can retrieve images from a database, perform math, edit text, perform logic, create lists, create variables or implement functions. Rules implement business logic for the execution and managing of data.

Use the visual rules editor within your application to create business logic for your application. The editor uses the Google Blockly visual code editor, which is easy to understand when creating native script code. See Google Blockly Demos to familiarize yourself with Google Blockly.

You must have a valid Mobile Development Kit project with existing pages.

The following steps describe how to create a rule visually within your Mobile Development Kit application:

  1. Right-click the Rules folder and select New MDK Rule
  2. Provide a rule name and select Next to proceed.
  3. On the Confirmation page, select Finish.
  4. The Rule Editor displays.

For complex rule logic, it is recommended to write rules directly in JavaScript.

Rule Editor

The Rule Editor allows you to create a visual representation of your business logic which generates JavaScript code.

Use the blocks to create a rule that enforces business logic (do something), create a navigation path, compute field values based on the following categories:

  • Logic – if, Boolean
  • Loops – do something for a certain count
  • Math – simple to advanced math
  • Lists – create list, get a list item
  • Variables – create and alter variables
  • Functions – create functions for reusability
  • Text – simple to advanced text manipulation
  • Library – contains randomize block
  • System – manipulation of data and time
  • Client APIs – can query application state and data
  • Predefined – where predefined rules are stored

See Customizing Advanced Rules for more details.

When saving your rule, there are two files associated with each rule. One has the extension .rule and the other has a rule.js extension. The .rule file is the visual representation of the code while the rule.js contains the JavaScript code. If you edit the rule.js file, the visual representation, .rule content does not change. However, if you change your rule within the .rule file, you overwrite the rule.js file.

Create a Simple Rule

This validation rule determines if an editable field is blank when you try to save it. It returns True or False depending on the content of the field. This example displays a message to the user so that it can be fixed. Message actions are not always required.

Context

You have opened the MDK Rule Editor from Create Rules.

Procedure

  1. Select the Functions from the left column of the rule editor and select the if [puzzle piece] return [puzzle piece]. Drag this piece into the editor.

  2. Fill in the first empty puzzle piece to determine what you are comparing:

    • Navigate to the Client APIs menu on the left side of the editor. Use the puzzle piece that says clientAPI.binding and drag it into the first empty puzzle piece.
    • There is a pull-down menu next to the word binding, select evaluateTargetPath. Tell the target path where to go by selecting the Object Browser from Client APIs and navigate to the field to verify if it is blank.
    • On the Object Browser page, change OData Collections to UI Properties and search for the field that is being validated on the edit page.
    • Double-click the field so that the expression is entered in the box on the bottom of the page and select OK.
  3. The evaluateTargetPath returns True if there is content in that field or False if it is blank. Go to the Boolean section and drag the True puzzle piece on the same line as the evaluateTargetPath and False on the next line.

  4. Warn the user that field is empty by creating a message action. Right-click on the Actions folder and select New MDK Action.

  5. On the Template Section page, choose Message Action and select Next.

  6. On the new page, provide a name for the action. The type is Message because you must do something. In this case, go back and fix the problem so the entity can be saved. Provide a friendly message and a title, and OK in the OK caption field. Select Next to proceed.

  7. On the Confirmation page, select Finish.

  8. Add the rule and the warning message to the affected page. Find the Update action for the page we edited under the Actions folder. This page contains the action that saves the data, select that action.

  9. Navigate to Common Action Properties and find Validation Rule. Select the Object Browser icon on the far right and select the rule that was recently created. Select OK.

  10. Find the Validation Failure Action and from the Object Browser, select the new failure action that was recently created. Select OK to finish.

  11. Don't forget to save your project and test your rule!

For advanced usage of rules, see Customizing with Advanced Rules

For use of built-in NativeScript modules, see NativeScript Framework Modules


Last update: March 19, 2020