Experience Salesforce
Boolean Datatype in Salesforce
What You’ll Learn
- What is Boolean Datatype?
- Boolean Methods
Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
In Salesforce, the Boolean data type represents a logical entity that can have one of two values: true or false. This is useful for fields that require a simple yes/no or on/off value.
Examples:
Checkbox Field: When you create a custom checkbox field on an object, the backend is a Boolean field. The user interface shows a checkbox to represent true/false values.
// Define a Boolean variable
Boolean isEligible = false;
// Change the value based on some condition
if (someCondition) {
isEligible = true;
}
// Use the Boolean in a decision
if (isEligible) {
// Do something if eligible
}
What is Boolean Datatype?
A boolean variable can only be assigned a true, false, or null value. A boolean variable is used in control statements to determine the flow of a program. It’s commonly utilized in decision-making structures to control the flow of code execution.
Characteristics of Boolean Data Type in Salesforce
- Storage: Boolean values are stored as binary states, either true (1) or false (0).
- Use Cases: Commonly used for checkboxes in Salesforce records, where a checkbox checked state represents true, and an unchecked state represents false.
- Field Creation: When creating a custom field in Salesforce, you can choose the “Checkbox” data type, which inherently uses Boolean values.
- SOQL (Salesforce Object Query Language): Boolean fields can be queried directly in SOQL. For example, to find all records where a Boolean field is true, you might write:
SELECT Id, Name FROM Account WHERE IsActive__c = true |
- Apex Programming: In Apex, Salesforce’s programming language, the Boolean data type is used similarly to other programming languages. An example in Apex:
Boolean isActive = true;if (isActive) { System.debug(‘The record is active.’);} |
- Formulas and Validation Rules: Boolean fields can be used in formulas and validation rules. For instance, a validation rule might ensure that a Boolean field is true before allowing a record to be saved.
Boolean Methods
The following are methods for Boolean. All methods are static.
S.No | Function | Example |
1 | valueOf(stringToBoolean)Converts the specified string to a Boolean value and returns true if the specified string value is true. Otherwise, returns false. | Boolean b = Boolean.valueOf(‘true’);System.assertEquals(true, b); |
2 | valueOf(fieldValue)Converts the specified object to a Boolean value. Use this method to convert a history tracking field value or an object that represents a Boolean value. | List<AccountHistory> accountHistoryList= [SELECT Field,OldValue,NewValue FROM AccountHistory]; for(AccountHistory accountHistory: accountHistoryList ) { System.debug(‘Field: ‘ + accountHistory.Field); if (accountHistory.field == ‘IsPlatinum__c’) { Boolean oldValue = Boolean.valueOf(accountHistory.OldValue); Boolean newValue = Boolean.valueOf(accountHistory.NewValue);}} |
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.