if (standardSearch = true) {
You are performing an assignment with =
inside this if
statement. Use ==
to compare for equality.
As a guard against this kind of mistake, you can write the constant first:
if (true == standardSearch) {
Then you get an error if you write the wrong number of = signs.
sfdxfox rightly notes that simply referencing Booleans without a comparison in a truth context, as if (standardSearch)
or if (!standardSearch)
is in many ways a better idiom that avoids this issue entirely. You may in a few cases want to make the comparison explicit as above - if you might have a null
value, certainly, or in highly complex logical expressions where the more verbose form is more readable or clear.
The constant == variable
pattern is one that applies equally in other contexts where you're making such a comparison against an lvalue (an expression to which a value may be assigned). For example,
if ('foo' == myStringVariable) {
if (2 == myIntegerVariable) {
if (2.0 == mySobjectList[1].Some_Currency_Field__c) {