Typically, every business app enforces rules that prevent bad data from getting into the system. Without such rules, things can get real messy, real fast, because users might not adhere to these rules on their own. In this tutorial, you learn how to enforce a basic business rule for the Warehouse app: No one can order more items than are currently in stock. To do this, you create and test a validation rule, all in just a couple of minutes without any coding.


You must complete all previous tutorials in this series, starting with Quick Tutorial 1: Build a Cloud App in 5 Minutes.

Step 1: Understand the Business Rule

Before you begin, make sure you have a clear understanding of this particular business rule. First, log into your DE org, switch to the Warehouse app, then view a specific line item within an Invoice. Play around with the quantity field for the line item. Notice that a value is required, but that you can set the value to any number: 1,000,000, 0, -10. That's not acceptable.

Qs5 1.png

Step 2: Create a Validation Rule

Enforcing basic business rules with Force.com is easy and doesn't require any coding. To do this, create a validation rule. Using the handy Force.com Quick Access toolbar, click +New to the right of View Validation Rules.

Qs5 2.png

Fill out the top part of the Line Item Validation Rule form as shown below. Notice that the Description field is a good place for you to document what the rule does so that other developers can easily understand the purpose of the rule. Also notice that there are handy links to help for the page.

Qs5 3.png

Next, fill out the Error Condition Formula section of the form, as shown below, to define the rule's condition. Notice that the form has handy builders to help you get familiar with Force.com formula syntax, which is very similar to what you might be used to with a spreadsheet program's macro language. Also notice that you can enter formulas free form and then check your syntax to make sure it is valid.

Qs5 4.png

One other important point to understand: Build a validation rule's error condition formula to identify when the error condition is TRUE.

Finally, fill out the Error Message section of the form as shown below. Notice that you can specify the location for the error message. In this example, you set the message to appear below the Quantity field on the Line Items page layout.

Qs5 5.png

Click Save, then take a quick look at the Validation Rule Detail page. Notice that the new validation rule is "active," meaning that Force.com is enforcing the rule. In certain situations, you might want to deactivate the rule temporarily (for example, before loading a bunch of data). This is easy to do when you want to — just click Edit and uncheck Active.

Qs5 6.png

Step 3: Try Out the App

Now that the rule is in place and active, return to an Invoice record, click into a Line Item record, and attempt to set its Quantity field to -10, then a really big number, such as 1,000,000. Each time, the update fails and you see the error message that you set up for the rule.

Qs5 7.png

Tell Me More

The formula in this tutorial is rather simple. But don't let that fool you. Force.com's formula syntax empowers you to enforce a wide range of business rules. For a list of sample validation rules, make sure to read Examples of Validation Rules.

Qs5 8.png

Next Steps

Entering data records one by one to test the application is reasonable. But what if you want to load a lot of data quickly. In the next tutorial, you will learn how to load data into your Warehouse app's database.