function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vladimir Mir 1Vladimir Mir 1 

simplify: if( (opp.StageName == 'PO received') || (opp.StageName == 'Closed Won') ) {...}

Hi all. can you simplify the above to something similar to SOQL's "IN" operator? Do not want to duplicate field name for every potential value check 
Best Answer chosen by Vladimir Mir 1
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Vladimir,  I think this is what you're after (not the switch statement):
 
Set<String> stageNames = new Set<String>
{   'PO received'
,   'Closed Won'
//  add more here if necessary
};

if ( stageNames.contains( opp.StageName ) )
{
    //  do your thing here...
}

 

All Answers

Steven NsubugaSteven Nsubuga
switch on opp.StageName {
   when 'PO received' {
       System.debug('PO received');
   }
   when 'Closed Won' {
       System.debug('Closed Won');
   }
   when else {
       System.debug('default');
   }
}

 
Steven NsubugaSteven Nsubuga
That is the switch statement in Apex.
switch on opp.StageName {
   when 'PO received', 'Closed Won' {
       System.debug('something');
   }
   when else {
       System.debug('default');
   }
}


 
Raj VakatiRaj Vakati
You can able to do it in many ways .. But i dnt see any think wrong or performance issue in your code 

Option 1 : Using Collection 
 
Set<String> str = new Set<String>() ;
str.add('PO received');
str.add('Closed Won');

If(str.contains(opp.StageName)){
// Logic is here 
}

Option 2 : Using switch statement 
switch on opp.StageName {
   when 'PO received', 'Closed Won' {
       System.debug('something');
   }
   when else {
       System.debug('default');
   }
}

 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Vladimir,  I think this is what you're after (not the switch statement):
 
Set<String> stageNames = new Set<String>
{   'PO received'
,   'Closed Won'
//  add more here if necessary
};

if ( stageNames.contains( opp.StageName ) )
{
    //  do your thing here...
}

 
This was selected as the best answer