Add Business Logic
So far, your app lets employees securely enter and view data about suggestions. That’s pretty cool, but you can make the platform work a little harder for you so that your 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. Add a formula field to calculate the number of days from suggestion creation to implementation. Want to learn more about formulas and validations? Earn the Formulas and Validations badge.
- Click the gear icon and select Setup.
- Click the Object Manager tab.
- 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.
- Field Label:
Number of Days Open
- Field Name:
Number_Of_Days_Open
- Formula Return Type: Number
- Decimal Places:
0
- Leave all other options as the default.
- Click Next.
- Click the Advanced Formula tab.
- In the Formula Editor, enter the formula:
IF(ISBLANK(Implemented_Date__c), TODAY() - DATEVALUE(CreatedDate), Implemented_Date__c - DATEVALUE(CreatedDate))
- To ensure there are no errors, Click Check Syntax.
- Click Next and Next again.
- 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.
- From the list of objects in Object Manager, select Suggestion.
- Select Validation Rules, then click New.
- Enter the rule details.
- Rule Name:
Date_in_Range
- Error Condition Formula:
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
- Leave all other options as the default.
- Click Save.
Add a Field Update Using Flow Builder
Let’s make it easy on the person who has to act on all the suggestions. When a user adds an implemented date, this automation will automatically update the status to Implemented. Want to learn more about all the automation tools? Complete the Build Flows with Flow Builder trail.
- From Setup, click the Home tab.
- Enter
flow
in Quick Find, then select Flows.
- Click New Flow.
- Select Start from Scratch and click Next.
- Select Record-Triggered Flow and click Create.
- For Object, select Suggestion.
- Select A record is created or updated.
- For Condition Requirements, select All Conditions Are Met (AND).
- For Field, select Implemented_Date__c.
- For Operator, select Is Null.
- For Value, select $GlobalConstant.False.
- For When to Run the Flow for Updated Records, select Only when a record is updated to meet the condition requirements.
- For Optimize the Flow for, select Fast Field Updates.
Add an Update Records Element and Activate the Flow
Now add the update triggering record action when the conditions are met.
- On the flow canvas, on the path after the Start element, click .
- Select Update Records.
- Label the new Update Records element as
Update Status to Implemented
.
- For How to Find Records to Update and Set Their Values, ensure Use the suggestion record that triggered the flow is selected.
- For Set Filter Conditions, ensure None-Always Update Record is selected.
- In the Set Field Values for the Suggestion Record section, for Field, select Status__c.
- For Value, select Implemented.
- Click Save in the upper right.
- Label your new Flow as
Update Status
.
- Ensure the API Name is
Update_Status
.
- Click Save.
- Click Activate.
- Click the back arrow button in the upper left to leave Flow Builder.
Try Out the App
Your business logic fires whenever a record is created or updated.
- Go back to a Suggestion record under the Suggestions tab (navigate to the App Launcher and select Suggestion Box).
- 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.
Resources