đź“Ł Attention Salesforce Certified Trailblazers! Link your Trailhead and Webassessor accounts and maintain your credentials by December 14th. Learn more.
close
trailhead

Add Business Logic

What You’ll Do

  • Create a custom formula field to calculate the length of time between two dates.

  • Add a validation rule to enforce that the implemented date is not in the future.

  • Create a process to update a field when the value of another field changes.

  • Try the app in the browser or Salesforce mobile app.

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.

Step 1: 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 Gear icon and select Setup. Click the Object Manager tab next to Home.

  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, type the following 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. Click Next and Next again to accept the default field visibility and security settings.

  8. Click Save to save the Number of Days Open field and add it to the Suggestion layout.

Step 2: 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 Gear icon and select Setup. Click Object Manager tab next to Home.

  2. From the list of objects, select Suggestion. Select the Validation Rules section from the left navigation, 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.

Step 3: 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 typing the following in 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 click the radio button next to 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. In the upper right corner, click Activate and then click Confirm.

Step 4: Try Out the App

Your business logic fires whenever a record is created or updated from the browser or the mobile app. Go back to a Suggestion record under the Suggestions Tab (navigate to the App Launcher and select the Suggestion Box app) and enter a date in the Implemented Date field. Try a date in the future. Did the system block you from saving? 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

retargeting