Skip to main content
La Trailblazer Community en Slack ya está en fase beta abierta. Únase y conéctese en tiempo real. Únase a nosotros en Slack

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.

  1. In your Playground, in Setup, open the Object Manager and select Contact.
  2. Select Fields & Relationships and select New.
  3. Select Formula and Next.
  4. For Field Label, enter Do Not Contact. Field Name populates automatically.
  5. Select Checkbox and Next.
  6. Select Advanced Formula.
  7. From Functions, insert the AND function.
    Your formula should be:
    AND(logical1,logical2,...)
  8. For AND parameter logical1 insert the Do Not Call field.
  9. For the logical2 parameter insert the Email Opt Out field.
  10. Remove the extra ,... since you only need two options.
    Your formula should be:
    AND( DoNotCall , HasOptedOutOfEmail )
  11. Select Check Syntax.
  12. For Description enter Combined do not contact check.
  13. 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.

Note

There’s more than one way to write the AND function. You can write it as a function with AND or as an operator by linking two statements with &&. In this case,

AND(DoNotCall, HasOptedOutOfEmail)

is equivalent to

DoNotCall && HasOptedOutOfEmail.

Make sure not to mix up &&, which replaces AND, with &, which concatenates two text strings.

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.

  1. In Setup, open the Object Manager and select Contact.
  2. Select Fields & Relationships and select New.
  3. Select Formula and Next.
  4. For Field Label, enter Is Executive. Field Name populates automatically.
  5. Select Checkbox and Next.
  6. Select Advanced Formula.

Now, you build the function.

  1. From Functions, insert the OR function.
    Your formula should be:
    OR(logical1,logical2,...)
  2. Remove the extra ,... since you only need two options.
  3. For OR parameter logical1 insert the BEGINS function.
    Your formula should be:
    OR( BEGINS(text, compare_text) ,logical2)
  4. Replace the BEGINS parameter text with the Title field.
  5. Replace the compare_text parameter with "Chief".
    Your formula should be:
    OR( BEGINS( Title, "Chief") ,logical2)
  6. For the logical2 parameter insert the CONTAINS function.
  7. For the CONTAINS parameter text insert the Title field.
  8. Replace the compare_text parameter with "President".
    Your formula should be:
    OR( BEGINS( Title, "Chief") , CONTAINS( Title, "President"))
  9. Select Check Syntax.
  10. For Description enter Executive level check.
  11. Click Next, Next and Save.
Note

Just as with the AND function, you can write OR more than one way. You can write
BEGINS(Title , "Chief") || CONTAINS(Title , "President").

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)
  )
)
Note

Because spaces and returns between functions and operators don’t matter in formulas, the line breaks and spaces shown here aren’t necessary. However, using line breaks and spaces in your formula makes it easier to read. It also helps anyone else who has to understand or update the formula later.

Now you build this out. You got this!

  1. In Setup, open the Object Manager and select Contact.
  2. Start a new formula checkbox field labeled Contact Complete set as a checkbox.
  3. In the Advanced Formula field start by inserting the NOT formula.
  4. For the logical parameter insert the OR function.
  5. 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)
  )
)
  1. For each of the expression parameters 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)
  )
)
  1. Select Check Syntax.
  2. For Description enter All fields for a complete contact record and filled in.
  3. For Blank Field Handling select Treat blank fields as blanks.
  4. 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.

Note

NOT can also be written as a simple exclamation point. Using this notation, our formula would look a little different, but have the same effect.

!(
  OR(
    ISBLANK(FirstName),
    ISBLANK(LastName),
    ISBLANK(Phone),
    ISBLANK(Email),
    ISBLANK(MailingAddress))
)

You’re just getting started with the different data types. Next up is Numbers!

Resources

Comparta sus comentarios sobre Trailhead en la Ayuda de Salesforce.

Nos encantaría conocer su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios cuando quiera desde el sitio de la Ayuda de Salesforce.

Más información Continuar para compartir comentarios