Understanding Boolean Values in Apex
Have you ever answered a Yes/No question? That's exactly what Boolean values are in computer programming! In Apex (Salesforce's programming language), Boolean values are simply true
or false
- nothing else.
Think of Boolean values like light switches - they're either ON (true
) or OFF (false
).
What Are Boolean Values Used For?
Booleans help your program make decisions, just like how you decide things in everyday life:
- Is it raining outside? Yes (
true
) or No (false
) - Is the store open? Yes (
true
) or No (false
) - Do I have enough money? Yes (
true
) or No (false
)
Creating Boolean Variables
To create a Boolean in Apex, you use the word Boolean
followed by a name, then assign it either true
or false
:
// Creating a Boolean that is true Boolean isRaining = true; // Creating a Boolean that is false Boolean isStoreOpen = false; // You can also declare first, then assign later Boolean doIHaveEnoughMoney; doIHaveEnoughMoney = true;
Changing Boolean Values
You can change a Boolean's value anytime in your code:
// Start with the store being closed Boolean isStoreOpen = false; // Later in your code, the store opens isStoreOpen = true; // Later still, the store closes isStoreOpen = false;
Simple Comparisons
Booleans are often created by comparing things:
// Is 5 greater than 3? Boolean isFiveGreaterThanThree = (5 > 3); // This will be true // Are these two names the same? Boolean areNamesEqual = ('John' == 'John'); // This will be true // Is 10 less than 7? Boolean isTenLessThanSeven = (10 < 7); // This will be false
Using AND, OR, and NOT
You can combine or flip Boolean values:
// AND (&&): Both sides must be true for the result to be true Boolean hasUmbrella = true; Boolean isRaining = true; Boolean shouldUseUmbrella = hasUmbrella && isRaining; // true because both are true // OR (||): At least one side must be true for the result to be true Boolean hasCar = false; Boolean hasBusPass = true; Boolean canGetToWork = hasCar || hasBusPass; // true because at least one is true // NOT (!): Flips true to false, or false to true Boolean isWeekend = false; Boolean needToWork = !isWeekend; // true because isWeekend is false
Making Decisions with Booleans
Booleans control which parts of your code run:
Boolean isCustomerPremium = true; // This is called an "if statement" if (isCustomerPremium) { // This code only runs if isCustomerPremium is true System.debug('Welcome premium customer!'); } else { // This code only runs if isCustomerPremium is false System.debug('Welcome regular customer!'); }
Converting Between Booleans and Text
Sometimes you need to convert between Boolean values and text:
// Converting text to Boolean Boolean b1 = Boolean.valueOf('true'); // Becomes true Boolean b2 = Boolean.valueOf('false'); // Becomes false Boolean b3 = Boolean.valueOf('anything else'); // Becomes false // Converting Boolean to text String s1 = String.valueOf(true); // Becomes 'true' String s2 = String.valueOf(false); // Becomes 'false'
Real-World Examples
Here are some ways Booleans are used in Salesforce:
// Setting a checkbox field on a Contact contact.DoNotCall = true; // Do not call this customer update contact; // Checking if a user has permission to do something Boolean canUserEditRecords = UserInfo.isCurrentUserLicensed('PermissionName'); // Validating if required fields are filled in Boolean isFormComplete = (firstName != null && lastName != null);
Remember
- Booleans can only be
true
orfalse
- They're perfect for yes/no situations
- Use meaningful names so it's clear what your Boolean represents
- Booleans help your program make decisions
Now that you understand Boolean values, you'll see them everywhere in programming!
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck