Saltar al contenido

Introduction to App Builder - Lesson 7: More about rules

This is the seventh lesson of the Introduction to App Builder tutorial series.

We've been using rules to develop our Northwinds application since Lesson 3, but now we'll deepen our understanding of them by getting acquainted with a few more concepts surrounding rules and their usage.

Default rules

If you want certain pieces of data to be added by default to new record(s) as soon as they are created, you need to use what we call a default rule. For example, if you're adding a new product, there needs to be a default price.

Default rules are created in the same area of App Builder where you build other rules, but before we create our first default rule, we'll take a look at another important concept: joins.

Joins

When we're creating rules, it is sometimes necessary to draw data from two separate tables. To do this, we join the tables together, using what we call a join. There are three types of joins: inner, left outer, and right outer.

Join types

  • Inner join: An inner join only returns the values that exist in both tables.

  • Left outer join: A left outer join returns all the values from the left table, regardless of whether or not they also appear in the right table.

  • Right outer join: A right outer join returns all the values from the right table, regardless of whether or not they also appear in the left table.

Create a default rule

Let's create a default rule for the example we used above: we don't want the end users of our Northwinds application to set a unit price. Therefore, we'll create a rule that pulls the unit prices from the data source.

In Lesson 6, we've already set the Unit Price controls in the application UI to be read only, so now we'll create a rule that specifically dictates this value.

  1. Go to App Workbench > Rules and click + Rule.

  2. Following App Builder's naming conventions, give it the name OrderDetail (Default UnitPrice).

  3. In the Purpose field, select Default.

  4. In the Target field, select the OrderDetail table.

  5. Click Create.

  6. Once App Builder creates the rule, you'll see the OrderDetail table in the Tables tab. We'll need the OrderID and ProductID columns.

  7. Still in the Tables tab, click + Tables and select the Product table as well. It will appear linked to the OrderDetail table, because they are connected by a foreign key relationship.

  8. Select the UnitPrice column in the Product table.

  9. App Builder will automatically write the join, but click the Joins tab to make sure there are no errors:

    Joins

  10. As with all rules, it's always a good practice to validate it by clicking the Results button. You should see three columns, with one being the unit price for a given product.

Add a default rule to a panel

The default rules must be connected to a panel in order to have any effect. So let's return to the Orders page edit screen and select the OrderDetails panel.

  1. Click More > Defaults. The Defaults dialog opens.

  2. Click Register and another dialog will open, with a few options:

    1. In the Type field, select Rule.

    2. In the Rule field, select OrderDetail (Default UnitPrice).

    3. In the Binding field, select Implicit.

      Note

      To learn more about the two binding options, see Implicit and explicit binding.

    4. Click Save.

If you return to the Orders page preview now and try to add a new entry in the Order Details panel, you'll see that now the Unit Price is filled in automatically when you make a selection in the Product field, meaning your Default rule is working.

List retrieves

List retrieves are a way of expanding the functionality of list controls. A regular list control allows you to pull the ID value of a given record. A list retrieve enables you to also pull other values. We will demonstrate how list retrieves work by improving our existing Product (List) rule.

  1. Navigate to the Product (List) rule by finding it in App Workbench > Rules.

  2. In the Columns tab, click + Column.

  3. In the Column or Expression field, enter P.UnitPrice.

  4. In the Target field, select UnitPrice.

  5. Click Save.

  6. Now let's update the control that uses this rule. Navigate to the Orders page edit screen by finding it in App Workbench > Pages.

  7. In the Order Details panel, select Controls to see the list of controls present in the panel.

  8. Double-click the row of the Product control.

    Note

    You can also access these same options by previewing the Orders page with the View Page button, then opening the live designer in the action drawer and selecting the Product control.

  9. In the List Options tab, click the Retrievals button.

  10. In the Retrievals dialog, click + Add Retrieval.

  11. In the Panel Column field, select UnitPrice. In the (List Source) Column field, select UnitPrice.

  12. Click the confirmation icon to save and exit out of the dialog.

App Builder will take a few moments to save. After it does, return to the Orders page preview and try editing one of the products in the Order Details page, you'll see that the Unit Price control gets automatically updated based on your selection.

Validation rules

Another type of rule you can leverage are validation rules. As implied the name, they can be applied to certain columns to ensure that only data of a specific kind can be stored in them. Validation rules can be applied directly to tables in the data layer or to business objects in the business layer.

We'll demonstrate how validation rules work by creating one that rejects any discounts greater than 10%. To do so, we're going to use a WHERE clause.

  1. Go to App Workbench > Rules.

  2. Click + Rule to create a new rule.

  3. Following App Builder's naming conventions, give it the name OrderDetail (Discount Validation).

  4. In the Purpose field, select Validation.

  5. In the Target field, select OrderDetail.

  6. Click Create.

  7. In the Where tab, click + Where Clause. A dialog opens where you can create WHERE clauses:

    Where clause dialog

  8. In the Left Expression field, enter OD.Discount.

  9. In the Operator field, select >.

  10. In the Right Expression field, enter .1.

  11. Click Save and exit out of the dialog. App Builder has created the validation rule.

Now that the validation rule has been created, let's make sure it applies to the business object we created it for.

  1. Go to App Workbench > Rules.

  2. Select OrderDetail (Source), the business object that holds discount values.

  3. Click Events. A dialog called All Events opens.

  4. In the Save row, you can see which things take place when the end user saves a new record. That's where we need to apply the Validation rule we've just created. Click Rule Event Detail in that row.

  5. In the Validations panel, click Register. The Validation dialog opens:

    Validation dialog

  6. In the Type field, select Rule.

  7. In the Rule field, select OrderDetail (Discount Validation), the validation rule we've just created.

  8. In the Binding field, select Implicit.

    Note

    To learn more about the two binding options, see Implicit and explicit binding.

  9. In the Message field, enter which message will be displayed to end users if they try to enter invalid data. This field is mandatory.

  10. Leave the other fields with their default values and click Save to exit out of the dialog.

Return to the Orders page preview and try to add a new product in the Order Details panel with a discount greater than 10%. When you try to save, you should see an error show up, along with the error message you typed in.

Application level parameters

Instead of limiting the discount that users can apply directly through a rule like we just did, we can also make the discount limit a value that users can modify. The way that we can do that is by adding it as an application level parameter and storing it in the Parameter table.

  1. In App Workbench > Pages, navigate to the Parameter page, which we created back in Lesson 4.

  2. In the Panel tab, find the Panel Source field and click the table icon. This will take you to the Parameter table edit screen.

  3. In the Columns tab, add a new column by clicking + Column.

  4. Give it the name DiscountMaximum.

  5. In the Logical field, select Number.

  6. In the Physical field, select Decimal.

  7. Click Save.

  8. Now, let's add the new column to the business object that's based on the Parameter tab. You can either find it in App Workbench > Rules or by returning to the previous screen and then clicking the business logic icon next to the Panel Source field.

  9. Once you're at the Parameter (Source) rule edit screen, go to the Tables tab and find the new DiscountMaximum column in the list of columns from the Parameter table. Click the checkmark to select it.

  10. Now return to the Parameter page, either by finding it in App Workbench > Pages or by returning to the previous screen. Add the new DiscountMaximum column to the list of controls in the Parameter panel.

  11. Now in the Parameter page preview, use the Edit button to choose whatever maximum discount you want. For instance, enter .3 to allow discounts of up to 30%.

Edit the validation rule to use the new parameter value

Even though we want to allow our users to determine the maximum discount, the validation rule we created before uses a fixed, hard-coded maximum discount, so it still won't tolerate any discounts bigger than 10%. Let's edit it to use whatever value we enter in the Parameter page. Follow these steps:

  1. Go to App Workbench > Rules and open the OrderDetail (Discount Validation) rule.

  2. In the Tables tab, click + Tables.

  3. Click Add for the Parameter table.

  4. Now that the Parameter table is also being displayed in the Tables tab, select the DiscountMaximum column.

  5. Go to the Where tab. We need to edit the WHERE clause we created because we no longer want to use a hard-coded value here. Click the pencil icon to edit it.

  6. In the Right Expression field, enter P.DiscountMaximum. App Builder will take a moment to save.

  7. Now, revisit the Orders page preview. If you try adding a new product in the Order Details panel, you'll see that now you are allowed to give discounts of up to 30%.

If you try to give a discount above 30% now, you won't be allowed to. However, the error message still says the previous limit, 10%, because we also hard-coded that when we first created the validation rule. To avoid having to edit the message every time the limit changes, we are going to use dynamic substitution to make the message be automatically updated along with the limit.

  1. In App Workbench > Rules, navigate to the OrderDetail (Source) rule.

  2. This rule currently only pulls data from the OrderDetail table, so in the Tables tab, click + Table and select the Parameter table. When it appears, select the DiscountMaximum column.

  3. Now the rule has access to the latest maximum discount and will be able to "see" when it changes. It is good practice to give it an Alias that we'll use to refer to it in the message, so enter MaxDiscount in that field. To make sure the number will be displayed correctly in the UI, go to the Columns tab and cast it as a Percent.

  4. Next, navigate back to the error message we wrote previously by heading to the Rule panel and clicking Events. When the dialog opens, find the Save row and click Rule Event Detail. The validation we implemented is shown in the Validations panel. Click to edit it.

  5. Finally, in the Message field, replace the hard-coded 10% with {{ MaxDiscount }}. Surrounding the alias with the double curly brackets will make App Builder apply dynamic substitution to show the current maximum discount instead of a fixed, unchangeable value.

CRUD rules

The last kind of rule we'll discuss is CRUD rules, which are used to update data in mass quantities. CRUD rules can be used to create, insert, update, or delete records, and you often need to do that in your App Builder applications. We'll look at how to work with CRUD rules and in the process, we'll see about two important functions. We'll work on the Employees page this time.

Create an Upper CRUD rule

  1. Go to App Workbench > Rules.

  2. Click + Rule.

  3. Give it the name Employee (FirstName Upper Update).

  4. In the Purpose field, select CRUD.

  5. A new field, Action, appears. Select Update.

  6. In the Target Layer field, select Data Layer.

  7. In the Target field, select Employee.

  8. Click Create.

  9. In the Tables tab, App Builder will automatically select the EmployeeID column, which is the primary key of the Employee table. We don't need any others, so go ahead and click + Column.

  10. When the Add Column dialog opens, enter the following syntax in the Column or Expression field: Upper(E.FirstName).

    Note

    This syntax invokes the Upper() database function, which takes a string as an argument to be capitalized and then returned. Several other database functions are available to use with App Builder.

  11. In the Alias field, enter UpperFirstName.

  12. In the Target field, select FirstName.

  13. Click Save.

Once App Builder saves the new rule, check to see if it's working by clicking Results in the Rule panel. The Upper function takes each employee's name and rewrites it using all capital letters.

Attach the new Upper CRUD rule to an event

It's always best to place your CRUD rules on the tables, because that helps streamline and organize the events, especially if you ever need to make updates or changes in the future.

  1. Locate the Employee (Source) rule in App Workbench > Rules and open it.

  2. Click Events.

  3. When the All Events dialog opens, click + Rule Event.

  4. Give it the name Upper First Name. You can leave the other fields with their default values. Click Save.

  5. When you are taken back to the All Events dialog, the new event you've just created should be available. Click the Rule Event Detail button.

  6. In the Actions panel, click Register.

  7. When the Action dialog opens, use the Rule field to select Employee (FirstName Upper Update), the CRUD rule we've previously created. Click Save.

  8. You will then see the confirmation page, showing details about the new event:

    UpperFirstName event

    Notice the Bindings panel. It shows which columns we've used to bind the records in the UI layer (the Source Column field) to the records in the rule (the Rule Column field). With this binding in place, the rule will only execute against records in the UI with corresponding values. Without binding, the rule would affect all rows returned in the CRUD rule.

Now that we've created an event, let's go add a button that executes it in our page.

  1. Navigate to the Employees page edit screen, and access the list of controls in the Employees panel.

  2. Click + Control to add a new control.

  3. In the Control Type field, select Button.

  4. In the Name field, enter First Name Upper. Click Next.

  5. In the Execute an Event field, select UpperFirstName, our newly-created event.

  6. Click Next and then Finish.

Now, access the Employees page preview and test the new button. Upon clicking it, an employee's first name should appear in all capital letters.

Practice time: Create a Lower CRUD rule

For practice, try to repeat these steps we've just followed, but this time creating a button that does the opposite process. Using the Lower() database function, create a button that changes employees' names to be listed in all lowercase letters.

Review and wrap-up

In this lesson, we have greatly expanded our knowledge of rules in App Builder, learning about default, validation, and CRUD rules, as well as application level parameters, and list retrieves. This concludes this tutorial series.

Your Northwinds application is now functional! It shows information about customers, the orders they place, employees, suppliers, and shippers in an organized and efficient way. The concepts and techniques you've learned during its construction can be applied to build any other application.