2

This may sound silly but I was wondering if we can use an IN in a IF condition.

I know we can do this in a query but suppose if I need to run multiple IF conditions on a particular object, it doesn't make sense to have multiple queries but have a single query and iterate over it.

So instead of

if(l.Status == 'Dead' || l.Status == 'Cold' || l.Status == 'Duplicate')

can I have something like

List.add('Dead','Duplicate','Cold');
if(l.Status IN List)

Is something like this possible?

  • In a language like Python, you can do if l.Status in ('Dead', 'Duplicate', 'Cold'): – Stephen S Oct 6 '17 at 14:06
7

You can create a Set that contains unique values and check if this Set contains value to check.

Set<String> availableStatuses = new Set<String> {
    'Dead',
    'Duplicate',
    'Cold'
    };

if(availableStatuses.contains(l.Status)) {}
  • 6
    +1 but. There's one pitfall with this approach: System.debug('a' == 'A'); System.debug(new Set<String>{'a', 'b', 'c'}.contains('A'));. Comparison of strings used in Sets (and as Map keys) is case sensitive so if you go this route - make sure to for example add your picklist vals lowercase and then also slap toLowerCase() on the value you're comparing to. – eyescream Oct 6 '17 at 7:34

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.