Learn from industry experts! Admin batch starts from 19 Feb.
Hurry up!
Book your slot now!

3

Boolean Datatype in Salesforce

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

  1. Storage: Boolean values are stored as binary states, either true (1) or false (0).
  1. Use Cases: Commonly used for checkboxes in Salesforce records, where a checkbox checked state represents true, and an unchecked state represents false.
  1. Field Creation: When creating a custom field in Salesforce, you can choose the “Checkbox” data type, which inherently uses Boolean values.
  1. 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
  1. 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.’);}
  1. 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.NoFunctionExample
1valueOf(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);
2valueOf(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);}}
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!