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.
Click and select Setup. Click the Object Manager tab next to Home.
From the list of objects, select Suggestion. Select the Fields & Relationships from the left navigation, then click New.
For the data type, select Formula and click Next.
Enter the field details, and then click Next.
- Field Label:
Number of Days Open
- Field Name:
- Formula Return Type: Number
- Decimal Places:
- Field Label:
Click the Advanced Formula tab.
In the Formula Editor, type the following formula:
IF(ISBLANK(Implemented_Date__c), TODAY() - DATEVALUE(CreatedDate), Implemented_Date__c - DATEVALUE(CreatedDate))
Click Check Syntax, to ensure there are no errors.
Click Next and Next again to accept the default field visibility and security settings.
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.
Click and select Setup. Click Object Manager tab next to Home.
From the list of objects, select Suggestion. Select the Validation Rules section from the left navigation, then click New.
Enter the rule details.
- Rule Name:
- 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
- Rule Name:
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.
From Setup, enter
Process Builderin Quick Find, then select Process Builder.
On the Welcome page, click New.
Enter the process details, and then click Save.
- Process Name:
- API Name:
Updates status to Implemented when Implemented Date field is populated.
- The process starts when: A record changes
- Click the Save button.
- Process Name:
Click Add Object
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.
Click Add Criteria.
Name the criteria
Select Formula evaluates to true.
Check whether the Implemented Date field is populated by typing the following in the Formula Editor:
NOT(ISBLANK( [Suggestion__c].Implemented_Date__c ))
Click Add Action.
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.
Leave No criteria—just update the records! selected.
Set the new field value.
- Field: Status
- Type: Picklist
- Value: Implemented
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.