Quick Tutorial 12: Add Basic App Logic — Add A Workflow Rule

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.

Step 1: Examine the Line Item Detail Page

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.

Qs12 line item.png

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.

Step 2: Create a Unit Price 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:

  1. For the data type select Currency and then click Next.
  2. Fill in the custom field details as follows:
    • Field Label: Unit Price
    • Length: 16
    • Decimal Places: 2
  3. Leave the defaults for the remaining fields by clicking Next on subsequent screens until you can Save.

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.

Qs12 line item price.png

Step 3: Create a Workflow Rule

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.

  1. Click New Rule.
  2. Select the Line Item object and click Next.
  3. For the rule name, enter Populate Unit Price and in the Description field enter something like Populates the Unit Price field on Line Item with the Price field on Merchandise.
  4. For evaluation criteria, select Only when a record is created.
  5. For rule criteria, select Line Item: Quantity, click the Operator list and choose greater or equal and in the Value field, enter 1.

Qs12 rule.png

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).

Step 4: Create a Workflow Action

Continuing on, the next step is to assign an action to the workflow rule: to update the Unit Price field automatically.

  1. In the wizard, click Save and Next.
  2. Click the drop-down list that reads Add Workflow Action and choose New Field Update.
  3. In the name field enter Copy Unit Price.
  4. In the Field to Update list, choose Line Item and then Unit Price.

Before continuing, confirm that your screen matches the following:

Qs12 field update.png

Now use a formula to derive the value to use.

  1. Select the option to Use a formula to set the new value.
  2. Click Show Formula Editor and then click Insert Field.
  3. In the second column, choose Merchandise > .
  4. In the third column choose Price.
  5. Confirm that your screen matches the following and then click Insert.

Qs12 merch formula.png

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.

Step 4: Activate the 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.

Step 5: Try Out the App

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.

QS12 merch price.png

Next Steps