Check Conditions with Checkboxes
Learning Objectives
After completing this unit, you’ll be able to:
- Use the AND, OR, and NOT functions in a checkbox formula.
- Describe use cases for formulas with the checkbox return type.
- Create a formula using the checkbox return type.
Before You Start
Before you start this badge, make sure you complete this content. The work you do here builds on the concepts and work you do in that content.
Gather More Data Insights with Formulas
You continue the formula journey that you started in Quick Data Insights with Formulas. Formulas are a quick way to create data insights by manipulating the data you already have access to. If you’re excited about what you can do with formulas, this badge is for you. You can use formulas to embed hyperlinks, perform calculations with field data, or apply conditional logic to display data in different ways. In this badge, you dig into working with the different data types and how to use them.
Formulas can do a lot within formula fields. Now you explore data types including how to choose a return type and how to convert it for display.
Checkbox Return Type
The checkbox data type holds a Boolean value: true or false. They’re great for easily understanding the condition of data. For example, to see right away whether an account has 500 or more employees, use this formula:
NumberOfEmployees >= 500
This is known as a conditional statement. It returns either a true or false response. If an account has 500 or more employees, the checkbox is checked on its page. Otherwise, it’s not checked. You can use checkboxes in Lightning pages to control dynamic forms, sections, or actions.
Most of the time, the conditional statements in your formula are more complicated than that. To take full advantage of the checkbox formula return type, you need some basic logical tools.
Logical Functions for Checkboxes
Logical functions are more rigid adaptations of words that you already use in almost every conversation: and, or, and not. For example, when you want to be sure this field AND that field have values. Or if only one field OR the other field has a value, but NOT if that other field has a value. You can use the same kind of logic in formulas with the AND, OR, and NOT functions.
For example, say you want your users to look at a contact and know right away whether it’s okay to contact them via email or phone. If the contact in question has marked Do Not Call and Email Opt Out, you want to select another checkbox, Do Not Contact. Because we want this checkbox to be checked only if the contact selected Do Not Call and Email Opt Out, we use the AND function.
Ready to Get Hands-on with Formulas?
Create a new Trailhead Playground now to follow along and try out the steps in this badge. Scroll to the bottom of this page, click the playground name, and then select Create Playground. It typically takes 3–4 minutes for Salesforce to create your Trailhead Playground. You also use the playground when it's time to complete the hands-on challenges.
Note: Yes, we really mean a brand-new Trailhead playground! If you use an existing org or playground, you can run into problems completing the challenges.
The AND Function
The AND function takes at least two arguments. It returns true if and only if all arguments are true. A table makes this logic easy to see.
First Argument = True | First Argument = False | |
|---|---|---|
Second Argument = True | True | False |
Second Argument = False | False | False |
Create a checkbox formula the same way that you would create any other formula field in Salesforce. Follow along and get hands on.
- In your Playground, in Setup, open the Object Manager and select Contact.
- Select Fields & Relationships and select New.
- Select Formula and Next.
- For Field Label, enter
Do Not Contact. Field Name populates automatically. - Select Checkbox and Next.
- Select Advanced Formula.
- From Functions, insert the AND function.
Your formula should be:AND(logical1,logical2,...) - For
ANDparameterlogical1insert the Do Not Call field. - For the
logical2parameter insert the Email Opt Out field. - Remove the extra
,...since you only need two options.
Your formula should be:AND( DoNotCall , HasOptedOutOfEmail ) - Select Check Syntax.
- For Description enter
Combined do not contact check. - Select Next, Next, and Save.
Using this formula, if someone has selected Do Not Call and Email Opt Out, the contact card displays another selected checkbox, Do Not Contact. If the person selected only one of the two, the Do Not Contact box is not checked.
If you want to check more than just two conditions, don’t worry—the AND function isn’t limited to two arguments. To display a contact with the Do Not Contact box checked on the condition that the person has selected Do Not Call, Email Opt Out, and Fax Opt Out is no problem for the AND function. Edit the Do Not Contact field and update the formula to:
AND(DoNotCall, HasOptedOutOfEmail, HasOptedOutOfFax)
Using this formula, the Do Not Contact checkbox is checked only if all three checkboxes—Do Not Call, Do Not Email, and Do Not Fax—are selected. If any of the three isn’t checked, Do Not Contact appears unchecked on the contact’s page.
The OR Function
The OR function is similar to the AND function. It also takes at least two arguments. But unlike AND, OR returns true if at least one of the conditions is true, and only returns false if all arguments are false.
First Argument = True | First Argument = False | |
|---|---|---|
Second Argument = True | True | True |
Second Argument = False | True | False |
Let’s look at another example with the contact object. You want to display a checkbox that’s selected if a contact is at the executive level of an organization. We know someone’s at the executive level if their title begins with “chief” (such as chief executive officer or chief operating officer) or includes the word “president” (such as president and vice president). Notice how we’re already using the word or to describe the problem. This is a good clue that you need the OR function in this formula.
To make this formula work, we’re going to use a couple of text functions that you will learn more about in the Use Text For Valuable Insights unit:
BEGINS(text, compare_text)returns true if a string (text) begins with another string (compare_text).CONTAINS(text, compare_text)returns true if a string (text) contains another string (compare_text) anywhere inside it.
Create a formula field on Contact with the return type Checkbox and the name Is Executive. Enter the following formula.
- In Setup, open the Object Manager and select Contact.
- Select Fields & Relationships and select New.
- Select Formula and Next.
- For Field Label, enter
Is Executive. Field Name populates automatically. - Select Checkbox and Next.
- Select Advanced Formula.
Now, you build the function.
- From Functions, insert the OR function.
Your formula should be:OR(logical1,logical2,...) - Remove the extra
,...since you only need two options. - For
ORparameterlogical1insert the BEGINS function.
Your formula should be:OR( BEGINS(text, compare_text) ,logical2) - Replace the
BEGINSparametertextwith the Title field. - Replace the
compare_textparameter with"Chief".
Your formula should be:OR( BEGINS( Title, "Chief") ,logical2) - For the
logical2parameter insert the CONTAINS function. - For the
CONTAINSparametertextinsert the Title field. - Replace the
compare_textparameter with"President".
Your formula should be:OR( BEGINS( Title, "Chief") , CONTAINS( Title, "President")) - Select Check Syntax.
- For Description enter
Executive level check. - Click Next, Next and Save.
The OR function, like AND, can take more than two arguments. Let’s check three options by also checking if a contact’s Title contains the word "Executive", to update the formula to:
OR( BEGINS( Title, "Chief" ) , CONTAINS( Title, "President" ) , CONTAINS( Title , "Executive" ))
The NOT Function
The NOT function does exactly what you’d expect—it returns true for false, or false for true.
Let’s review a use case. The sales team needs a way to know if a contact has all the essential contact information. You decide to display a checkbox on a contact’s page that indicates if the contact card has all the essential information. The sales team considers a contact complete if it includes a first name, last name, phone number, email address, and mailing address.
The way you find if a field is blank is by using the ISBLANK function. ISBLANK takes a field as an argument and returns true if it’s blank, and false if it’s filled. So, you can use the OR function to check if one of more fields ISBLANK.
OR( ISBLANK(FirstName), ISBLANK(LastName), ISBLANK(Phone), ISBLANK(Email), ISBLANK(MailingAddress) )
This formula returns true if any of the fields are blank. This is great but you want the checkbox to be checked when all the fields are complete. This is where the NOT function comes into its sweet spot. You can just wrap that function in the NOT function and get a true result when all the fields are completed.
NOT(
OR(
ISBLANK(FirstName),
ISBLANK(LastName),
ISBLANK(Phone),
ISBLANK(Email),
ISBLANK(MailingAddress)
)
)Now you build this out. You got this!
- In Setup, open the Object Manager and select Contact.
- Start a new formula checkbox field labeled
Contact Completeset as a checkbox. - In the Advanced Formula field start by inserting the NOT formula.
- For the
logicalparameter insert the OR function. - For each of the five fields you are testing add an ISBLANK function.
Your formula should be:
NOT(
OR(
ISBLANK(expression),
ISBLANK(expression),
ISBLANK(expression),
ISBLANK(expression),
ISBLANK(expression)
)
)- For each of the
expressionparameters of the ISBLANK functions insert one the five fields you are checking.
Your formula should be:
NOT(
OR(
ISBLANK(FirstName),
ISBLANK(LastName),
ISBLANK(Phone),
ISBLANK(Email),
ISBLANK(MailingAddress)
)
)- Select Check Syntax.
- For Description enter
All fields for a complete contact record and filled in. - For Blank Field Handling select Treat blank fields as blanks.
- Click Next, Next and Save.
And there you have it! You can test out this formula by looking at a contact in your organization. If all the relevant contact information is complete, the contact is marked as complete, and the Contact Complete checkbox that you created is selected. If one or more of the fields we checked is blank, the checkbox is unchecked.
You’re just getting started with the different data types. Next up is Numbers!
