Start tracking your progress
Trailhead Home
Trailhead Home

Add Business Logic

So far, your app lets employees securely enter and view data about suggestions on a browser or a mobile device. That’s pretty cool, but let’s make the platform work a little harder for us so that our users don’t have to.

Add a Formula Field

A formula field derives its value from other fields, expressions, or values, saving your users a lot of time. Let’s add a formula field to calculate the number of days from suggestion creation to implementation.

  1. Click Setupand select Setup. Click the Object Manager tab.
  2. From the list of objects, select Suggestion. Select the Fields & Relationships from the left navigation, then click New.
  3. For the data type, select Formula and click Next.
  4. Enter the field details, and then click Next.
    • Field Label: Number of Days Open
    • Field Name: Number_Of_Days_Open
    • Formula Return Type: Number
    • Decimal Places: 0
  5. Click the Advanced Formula tab.
  6. In the Formula Editor, enter the formula: IF(ISBLANK(Implemented_Date__c), TODAY() - DATEVALUE(CreatedDate), Implemented_Date__c - DATEVALUE(CreatedDate))  Screenshot of the New Custom Field screen with Step 3: Enter Formula as the title
  7. To ensure there are no errors, Click Check Syntax.
  8. Click Next and Next again.
  9. Click Save.

Add a Validation Rule

Formulas are also used to ensure data quality and enforce business rules. Our employees cannot predict the future, so let’s create a validation rule to make sure that they don’t enter implementation dates that haven’t happened yet. With validation rules, you define the condition for “bad” data. When the system evaluates the rule, it blocks the user from saving the data if the condition is true.

  1. Click Setupand select Setup. Click the Object Manager tab.
  2. From the list of objects, select Suggestion. Select Validation Rules, then click New.
  3. Enter the rule details.
    • Rule Name: Date_in_Range
    • Formula editor: Implemented_Date__c > TODAY()
    • Error Message: The implementation date must be today or in the past, not in the future.
    • Error Location: Field: Implemented Date
      Screenshot of the Validation Rule screen showing the error message The implementation date must be today or in the past, not in the future
  4. Click Save.

Add a Field Update Using Process Builder

Formulas are also useful when automating business processes. Let’s make it easy on the person who has to act on all the suggestions by saving them some time. When a user adds an implemented date, let’s update the status automatically to Implemented.

  1. From Setup, enter Process Builder in Quick Find, then select Process Builder.
  2. On the Welcome page, click New.
  3. Enter the process details, and then click Save.
    • Process Name: Update Status
    • API Name: Update_Status
    • Description: Updates status to Implemented when Implemented Date field is populated.
    • The process starts when: A record changes
    • Click the Save button.
  4. Click Add Object.
  5. Associate the process with the Suggestion object and specify when to start the process. For this process, select when a record is created or edited.
    Screenshot of the Update Status screen in Process Builder showing the option Choose Object and Specify When to Start the Process
  6. Click Save.
  7. Click Add Criteria.
  8. Name the criteria Implemented?
  9. Select Formula evaluates to true.
  10. Check whether the Implemented Date field is populated by entering the Formula Editor: NOT(ISBLANK( [Suggestion__c].Implemented_Date__c ))
    Screenshot of the Update Status screen in Process Builder showing the formula NOT(ISBLANK( [Suggestion__c].Implemented_Date__c )) in the Build Formula field
  11. Click Save.
  12. Click Add Action.
  13. Enter the action details, and then click Choose.
    • Action Type: Update Records
    • Action Name: Update Status to Implemented
    • Record Type: Click the magnifying glass to open the modal, then select Select the Suggestion__c record that started your process.
  14. Click Choose.
  15. Leave No criteria—just update the records! selected.
  16. Set the new field value.
    • Field: Status
    • Type: Picklist
    • Value: Implemented
      Screenshot of the Update Status screen in Process Builder showing the picklist values Status, Picklist, and Implemented
  17. Click Save.
  18. Click Activate and then click Confirm.

Try Out the App

Your business logic fires whenever a record is created or updated from the browser or mobile app. 

  1. Go back to a Suggestion record under the Suggestions tab (navigate to the App Launcher and select Suggestion Box).
  2. Enter a date in the Implemented Date field.
  3. Try a date in the future: Did the system block you from saving?
  4. Now try today’s date: The Status field should change to Implemented.

Screenshot of the Locker Room Suggestion record showing an incorrect date entry. The error message says that the date must be today or in the past, not in the future

Resources