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).
Booleans help your program make decisions, just like how you decide things in everyday life:
true) or No (false)true) or No (false)true) or No (false)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;
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;
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
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
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!');
}
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'
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);
true or falseNow that you understand Boolean values, you'll see them everywhere in programming!
// 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
// 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'
// 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);