This course is currently a work in progress. Please join the waitlist to receive regular updates.

Boolean variables in apex

What is an boolean variable?

In Apex, you can use boolean variables to store logical values. Boolean can hold true or false values. By default, boolean variables are initialized to null.

Think of boolean variables as questions that have a 'Yes' or 'No' answer. For example, when you visit a grocery store and the cashier asks, 'Do you need a plastic bag?' the answer is either 'Yes' or 'No.' In Apex, you could represent this decision with a boolean variable, where true means 'Yes' and false means 'No.'

How to declare a boolean variables?

You can declare a boolean using the Boolean data type and assign a value.

Loading...

In the above example:

  • The keyword Boolean is used to declare a boolean variable.
  • The variable name found is used to store a boolean value true.
  • The system.debug() statement is used to print the value.

When to use boolean variables?

Boolean variables are perfect for situations where you only need to know if something is "yes" or "no," "true" or "false,". For example, when using a checkbox, it can only have two values: checked (true) or unchecked (false).

In salesforce, the IsClosed flag on a case is a boolean field. It can only have two values: true, indicating the Case is closed, or false, indicating the Case is open.

Try It Yourself

Loading...