In my previous post
SFDC: Add a Rollup Summary Field, we see how to add roll-up summary field to aggregate the total amount for all line items in an invoice. Now here we see how to add validation rules so as to prevent bad invalid data to get into our system.
Typically, every business app enforces rules that prevent bad data from getting into the system. Without such rules, things can get really messy, really fast because users might not adhere to these rules on their own. In this post, we learn how to enforce a basic business rule for the Warehouse app—we can’t order zero or negative items. To do this, you create and test a validation rule, all in just a couple of minutes without any coding.
Understand the Business Rule
Before you begin, make sure you have a clear understanding of this particular business rule.
- Select the Warehouse app.
- Click the Invoices tab, select an invoice, and look at a specific line item.
- 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: 0, -10, and 3.14159. You don’t want users entering bad data (such as negative numbers), so this situation isn’t acceptable.
Create a Validation Rule
Enforcing basic business rules is easy and doesn’t require any coding.
- From Setup, click Create > Objects, click Line Item, scroll down to the Validation Rules related list, and click New.
- For Rule Name type Validate_Quantity.
- Optionally fill out the Description field. It’s a good practice to document business logic so that other developers can easily understand the purpose of the rule. Use the documentation links if you need extra help on this page.
- In the Error Condition Formula area, you build a validation rule’s error condition formula to identify when the error condition evaluates to TRUE.
a. Click Insert Field to open the Insert Field popup window.
b. Select Line Item > in the first column and Quantity in the second column.
c. Click Insert.
d. Type the less-than-or-equal-to symbol (<=) and the numeral 0, so the formula looks like:
Quantity__c <= 0
Click Check Syntax to make sure there are no errors. If you do find
errors, fix them before proceeding.
6. In the Error Message field, type You must order at least one item.
7. For the Error Location, select Field, then choose Quantity from the drop-down list.
Point to focus: Take a quick look at the Validation Rule Detail page. Notice that the new validation rule is “Active” meaning that the platform is currently enforcing the rule. Validation rules, unlike workflow rules, default to active. In certain situations, you might want to deactivate the rule temporarily (for example, before loading a bunch of data). This is easy to do by simply deselecting the Active box (but don’t do this now).
Try Out the App
Now that the rule is in place and active, it’s time to give it a try.
- Click the Invoices tab and select an existing invoice.
- Click New Line Item
- Enter a line item number and a quantity of –1.
- Once you choose a merchandise item and click Save, you’ll see the error message that you set up for the rule.
5. Fix the error by entering a valid quantity and then
Point to Focus:
- If you didn’t see the error message, check the validation formula again. You need to make the rule fire when the condition evaluates to TRUE.
- The formula in this tutorial is rather simple, but don’t let that fool you. The platform’s formula syntax empowers you to enforce a wide range of business rules that not only includes one object but pulls in other related objects as well.
Modify the Validation Rule
Modify the existing validation rule to check how many items are in stock.
- From Setup, click Create > Objects > Line Item, scroll down to the Validation Rules related list, and edit the Validate_Quantity rule.
- Edit the Description field to explain that it won’t allow users to order more items that are in stock.
- In the Error Condition Formula area, start by putting some parentheses around the first rule, insert the logical OR operator, and then add another set of parentheses so that the error condition looks like this:(Quantity__c <= 0) || ()
- Click on the second set of parentheses, then click Insert Field to open the Insert Field popup window.
- Leave Line Item > selected in the first column, select Quantity in the second column, and then click Insert.
- Type or insert the greater-than symbol (>).
- Click Insert Field and select Line Item > in the first column, Merchandise > in the second column, and Quantity in the third column.
- Click Insert and verify the code looks like the following: (Quantity__c <= 0) || (Quantity__c > Merchandise_r.Quantity__c)\
- Click Check Syntax to make sure there are no errors.
- Finally, edit the Error Message field to add You can’t order more items that are in stock, and then Save.
Take a look at the formula you created.
- Mechandise__r—Because the Merchandise object is related to the Line Item object, the platform lets you navigate from a line item record to a merchandise record; that's what the Mechandise__r is doing.
- Quantity__c—This is the field you created to track the total amount of stock on a merchandise record.
- Merchandise__r.Quantity__c—This tells the system to retrieve the value of Quantity field on the related merchandise record.
- Quantity__c—This refers to the Quantity field on the current (line item) record.
Putting it all together, the formula checks that the total inventory on the related merchandise record is less than the number of units being sold. As indicated on the Error Condition Formula page, you need to provide a formula that is true if an error should be displayed, and this is just what you want: it will only be true when the total inventory is less than the units sold.
Try Out the New Rule
Now that the modified rule is in place, test it.
- Click the Invoices tab and select an existing invoice.
- Create a New Line Item and type a quantity of 6000.
- Choose a merchandise item, and click Save. You see the error message that you set up for the rule.
- Fix the error by entering a valid quantity, and then click Save.
**This document is referred from
salesforce help tutorials