Boolean
Learning Content
Last updated: July 28, 2026
Boolean Data Type
The Boolean data type is used to store one of two possible values: true or false. It is one of the simplest yet most important primitive data types in Apex because it controls decision-making within your application.
Whenever your code needs to answer a yes-or-no question, such as "Is the user active?", "Has the payment been received?", or "Is the Opportunity closed?", a Boolean variable is the appropriate choice.
💡 LearnFrenzy Insight
Think of a light switch.
It has only two states:
- 💡 ON (true)
- 🌑 OFF (false)
A Boolean variable works in exactly the same way. It stores only one of these two values and helps Salesforce decide which path the program should follow.
What is a Boolean?
A Boolean is a primitive data type that represents a logical value. It can store only:
- true
- false
Boolean isActive = true; Boolean paymentReceived = false; Boolean isApproved = true;
Why Use Boolean?
| Reason | Benefit |
|---|---|
| Decision Making | Controls program flow using conditions. |
| Business Rules | Represents yes/no business decisions. |
| Validation | Checks whether conditions are satisfied. |
| Readable Code | Makes logic easier to understand. |
Variable Declaration
Boolean isLoggedIn = true; Boolean hasDiscount = false; Boolean isManager = true;
Using Boolean with if Statements
Boolean paymentReceived = true;
if (paymentReceived) {
System.debug('Order Confirmed');
}
Output
Order Confirmed
Using Boolean with Comparison Operators
Integer marks = 85; Boolean passed = marks >= 40; System.debug(passed);
Output
true
Common Comparison Operators
| Operator | Description | Example |
|---|---|---|
| == | Equal To | age == 18 |
| != | Not Equal To | status != 'Closed' |
| > | Greater Than | score > 80 |
| < | Less Than | price < 100 |
| >= | Greater Than or Equal To | marks >= 40 |
| <= | Less Than or Equal To | age <= 60 |
Logical Operators
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | isActive && isVerified |
| || | Logical OR | isAdmin || isManager |
| ! | Logical NOT | !isClosed |
Real Salesforce Business Example
🏢 Business Scenario
A company allows an Opportunity to move to the next stage only after manager approval.
Boolean isApproved = true;
if (isApproved) {
System.debug('Opportunity Approved');
} else {
System.debug('Approval Pending');
}
Output
Opportunity Approved
This type of logic is commonly used in approval processes, validation rules, and Apex triggers.
Common Salesforce Use Cases
| Business Requirement | Boolean Variable |
|---|---|
| User Active Status | isActive |
| Payment Received | paymentReceived |
| Manager Approval | isApproved |
| Record Locked | isLocked |
| Email Verified | emailVerified |
| Feature Enabled | featureEnabled |
Boolean vs Integer
| Boolean | Integer |
|---|---|
| Stores true or false | Stores whole numbers |
| Used for decisions | Used for calculations |
| Only two possible values | Large range of numeric values |
| Example: true | Example: 100 |
How Salesforce Uses Boolean Values
User Clicks Save
│
▼
Validation Checks
│
▼
Condition Evaluated
Is Approved?
│
Yes │ No
▼
Continue Stop
🔍 Behind the Scenes
Boolean isActive = true;
│
▼
Condition Evaluated
│
▼
Decision Made
│
├── TRUE → Execute Block
│
└── FALSE → Skip Block
- Use meaningful names such as
isActive,hasPermission, orisApproved. - Choose names that naturally read as true/false questions.
- Keep Boolean conditions simple and easy to understand.
- Reuse Boolean variables when the same condition is evaluated multiple times.
⚠️ Common Mistakes
- Using unclear variable names such as
flag1ortemp. - Comparing a Boolean to
trueunnecessarily (for example,if (isActive == true)instead ofif (isActive)). - Writing overly complex logical expressions that reduce readability.
- Using Integer values like 0 and 1 instead of Boolean variables to represent true/false states.
🎯 Interview Tip
Question: Why should you use a Boolean instead of an Integer to represent true/false values?
Answer: A Boolean clearly represents logical states using only true or false. It improves code readability, reduces mistakes, and makes conditional logic easier to understand and maintain compared with using numeric values such as 0 and 1.
- ✔ Stores only true or false.
- ✔ Used for decision-making and conditional logic.
- ✔ Frequently used with
if,else, and logical operators. - ✔ Represents business states such as approval, active status, or payment completion.
- ✔ Use meaningful names beginning with
is,has, orcanwhenever appropriate. - ✔ One of the most commonly used data types in Apex business logic.
➡ Next Lesson
In the next lesson, you'll learn about the String data type, understand how Apex stores and manipulates text, and explore common string operations used in real-world Salesforce applications.
Practice What You've Learned
Test your understanding with these practice exercises