Most apps rely on certain business logic to help ensure the quality of data. Some business logic is fairly common in most apps. For example, many fields in a database schema should be "automatic," thus relieving users from having to manually look up or enter data and perhaps make data entry errors. In this next series of tutorials, you're going to see how Force.com makes it easy to implement common business logic in your app without writing any code.
You must complete Quick Tutorial 1: Build a Cloud App in 5 Minutes before continuing with this tutorial.
To get started, quickly review the Invoice and Line Item objects from earlier tutorials. Log into you DE org, select the Warehouse app, click the Invoice tab, click any Invoice, and then load the detail page for a Line Item. Notice how there's no way to keep track of how much you are charging customers for merchandise in each Line Item.
In this tutorial, you create a new field in Line Item called Unit Price to track this data and then a workflow rule to automatically populate the new field.
From the Force.com Quick Access menu (the tab that pops out from the right side of the window), hover your mouse over View Fields and click +New to switch from your app to the New Custom Field wizard that's part of the Force.com development environment. (If you aren't on the Line Item object already, click <your_name> | Setup | Create | Objects. Then click the Line Item object and in the Custom Fields and Relationships section, click New.)
The steps for creating the new Unit Price field are essentially the same as when you created the Price field on the Merchandise object in Quick Tutorial 2: Build A Cloud Database — Add Fields to an Object, except this time name the field Unit Price:
After you finish, go back to an existing Invoice and add a new Line Item. Notice there's a new field for Unit Price, but you still have to populate that field manually.
To automatically populate the new Unit Price field, create a workflow rule.
Click <your_name> | Setup | Create | Workflow | Workflow Rules. Read the brief introduction on the Workflow page and then click Continue.
Populate Unit Priceand in the Description field enter something like
Populates the Unit Price field on Line Item with the Price field on Merchandise.
Note: It makes sense to fire the workflow rule only for new Line Items because you are effectively assigning a default field value when inserting the record. Later on, users should be free to adjust the price of Merchandise in each Line Item (for example, to offer discounts).
Continuing on, the next step is to assign an action to the workflow rule: to update the Unit Price field automatically.
Before continuing, confirm that your screen matches the following:
Now use a formula to derive the value to use.
In the formula, notice some new syntax, namely the "Merchandise__r". That last part is Force.com's object notation that you can use to traverse object relationships and access related fields. In this case, the formula uses the relationship between the Line Item record and Merchandise object to get the corresponding Merchandise record's value for Price.
Click Save, then click Done to return to the detail page of the new workflow rule.
By default, new workflow rules are not active. Therefore, click Activate.
Note: You might temporarily disable workflow rules in certain situations, such as before a bulk data load operation.
Now try out the revised app and see how the new workflow rule implements your business logic. Click the Invoices tab and either create a new Invoice or edit an existing Invoice. Then add a new Line Item. After you've chosen the Merchandise, click Save. If you click back into the detail page for the new Line Item, notice how the workflow rule automatically populated the Unit Price field by looking up the Price of the Merchandise that you selected.