Introduction
Formulas are one of Salesforce’s most powerful tools for manipulating, understanding, and exploring your data. From automating routine calculations to enforcing data best-practices, formulas are a key component of making your org work the way you want it to. But what do you do when one of those pesky errors stands between you and your vision?. In today’s post, we’ll walkthrough troubleshooting a formula error from start to finish, so you can learn what to ask, and how to react, when you’re not getting the results you expect.
Let’s dive in!
The Problem
Today’s example comes straight from the Trailblazer community, that wonderful resource where you can ask all of your Salesforce-related head-scratchers and get the solutions you need from the community of engaged experts.
When you have an idea for a formula, think about it in everyday language before you pull up the Advanced Formula Editor. Start by asking yourself what your goal is and describing it conversationally.
For example: Say that your goal is to ensure that if anyone other than a System Administrator marks an opportunity as “Closed Lost,” they have to provide an explanation.
For example, we assume that you already have the custom picklist field, Loss Reason, populated with reasons for losing an opportunity. If you don’t have that field, take a moment to create it. Because no one should be able to save a record without a reason, we use a validation rule formula to accomplish our goal.
In plain language, your formula is:
Show an error when the Stage of an opportunity is changed to “Closed Lost” by someone who isn’t a System Administrator, and Loss Reason is blank.
After you’ve written out your formula, look for keywords like “and,” “or,” and “if” that correspond to the functions used throughout this module. Meanwhile, these functions are the building blocks of larger, more complicated formulas. Recognizing them in your use cases helps you implement such formulas.
Advanced Formulas
AT first, to check that the user is not a System Administrator, insert $Profile.Name, a merge field that represents the profile making edits to the record. To check if StageName has been updated, use ISCHANGED(). ISCHANGED() returns true if the current value of a field differs from the previous value.
Also, check the rest of your requirements with ISPICKVAL(). For more information on ISPICKVAL(), see Using Picklists in Formulas.
- In Setup, use the quick find box to find the Object Manager.
- Click Opportunity | Validation Rules.
- Click New.
- In Rule Name, enter Loss Reason Required.
- In Error Condition Formula, enter the following formula: