0

I am stuck with this logic. I am getting the value of a from somewhere in below problem.

        if( a == 1 ) {
            b = 'Monthly';
        } else if (a == 3 ){
            b = 'Quarterly';
        } else if (a == 6 ){
            b = 'Semi-Annual';
        } else if (a == 12 ){
            b = 'Annual';
        } else if (a !== 1 || 3 || 6 || 12 ){
            b = 'Annual'; 
        }

Now if I use !== operator in last condition (a !== 1 || 3 || 6 || 12 ) the compiler throw me the error Exact equality operator only allowed for reference types: Decimal

and If I use != ie (a != 1 || 3 || 6 || 12 ). I get the error. Logical operator can only be applied to Boolean

I cannot leave else empty because I have few more conditions to check. Any help would be highly appreciated.

1
  • 1
    Hi Kashif, welcome to SFSE. Please take a moment to scroll through the tour and read How to Ask. Your post does not contain a question. – Adrian Larson Jun 28 '18 at 0:18
2

Apex does not allow chaining values for equality checks in the way you have written.

If you want to check if an integer matches one of several possible values, you either need to...

  • Explicitly compare the variable to each value (i.e. a != 1 && a != 3 && a != 6 && a != 12)
  • Find a common property to reduce the number of terms in your boolean expression (i.e. a != 1 && a <= 12 && Math.mod(a,3) == 0, though this doesn't exclude 9. This is just an example)
  • Check if the value exists in a map or set (i.e. Set<Integers> myInts = new Set<Integer>{1,3,6,12}; ...!myInts.contains(a)...)

Of course, if you already cover all of the possible conditions in your if/else if blocks, then you don't need to do anything for the final else if, you could instead convert it to a plain else (only one branch of an if/else if/else will be executed).

// I don't really know if there's a benefit to using strict equivalence with integers,
//   so, I'll just be using the normal equivalence check (==)
if(a == 1){
  // do something
}else if(a == 3){
  // do something
}else if(a == 6){
  // do something
}else if(a == 12){
  // do something
}else{
    // 'a' is guaranteed to not be 1, 3, 6, or 12 inside this else block
    // If you have more else-ifs that you omitted in your question, then
    //   'a' would be guaranteed to not be any of those things either.
}

In the simple case you've provided, there shouldn't be any reason not to use the final else block. If your if/else-if conditions are more than one term long, then the situation gets more complicated.

2

How about...

Map<Integer,String> labelsByMonthNumber = new Map<Integer,String> {
                          {1 => 'Monthly', 3 = 'Quarterly', 6=> 'Semi-Annual'};
String b = labelsByMonthNumber.containsKey(a) ? labelsByMonthNumber.get(a) : 'Annual';

two lines of code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.