Equals Operator - Strings
Not Started

Integers, Booleans, what are we going to compare next... Strings? Yes, literally that’s what we’re about to do.

Comparing Strings happens often as a Salesforce developer. We usually compare them to determine if a field has an expected value. Let’s say we want to check if an Opportunity Stage is “Closed Won”.

To do that, we’ll declare a variable to represent the current stage and use the == operator to compare the values.

String currentStage = 'Closed Won'; Boolean isClosedWon = currentStage == 'Closed Won'; // isClosedWon becomes true

We're directly comparing the variable currentStage to the String literal Closed Won and testing for equality.

The equals operator is case insensitive, so this code would have the same outcome:

String currentStage = 'Closed Won'; Boolean isClosedWon = currentStage == 'closed won'; // isClosedWon becomes true

You can also compare two String variables together like this:

String var1 = 'space'; String var2 = 'jam'; Boolean result = var1 == var2; // result becomes False

Out in the wild, you may see code that compares Strings using this syntax:

Boolean result = 'Closed Won'.equals(currentStage); Boolean result2 = 'Closed Won'.equalsIgnoreCase(currentStage);

This is valid syntax in Apex. The language provides a String Class with an array of built-in methods like the one you saw above. We won’t cover the String Class in this course, but If you’re curious to learn more, check out the link or ask us for a Strings deep dive course.

Challenge

A constant has been declared with the name ACTIVE_CUSTOMER_STATUS. Another variable accountStatus is keeping track of a particular account's status. Compare the two for equality and store the results in a variable named isAccountActive.