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
PamSalesforcePamSalesforce 

If condition in apex

I want to write an if statement like

if( x==1||x==2||x==3){}

Is it possible to write it like

if(x in [1,2,3]){}

or something similar...

if yes,please let me know how....

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

There is a concept that you can use to perform this type of test. For example:

 

 

if(new Set<Integer>{1,2,3}.contains(x)) { // this is equal to if(x==1||x==2||x==3) }

The idea is that you first create an anonymous Set, pre-populated with the conditions to check for, then use the contains instance method to compare a value you are looking for. This trivial example is actually longer than using the original code, but if you had a set of 50 values (a list of states in the United States, for example), this construct would be far more efficient. Strings are particularly efficient using this method:

 

 

if(new set<string>('red;green;blue;yellow;white;black;orange' .split(';') .contains(favoriteUserColor)) { // Same as // if( favoriteUserColor=='red' || favoriteUserColor=='green' || favoriteUserColor=='blue' || favoriteUserColor=='yellow' || favoriteUserColor=='white' || favoriteUserColor=='black' || favoriteUserColor=='orange') }

 

 As you can see, the larger the conditional statement, the more efficient the string comparison becomes.

 

 

All Answers

rubixtiousrubixtious
No.  IN is a SOQL keyword and cannot be used in a conditional statement.  You could do something like x <= 3, but I personally find that 'hacky' and hard to decipher when trying to learn what the code is doing 6 months later.
PamSalesforcePamSalesforce

I wish there was something like that in Apex...

Thanks for the help.

sfdcfoxsfdcfox

There is a concept that you can use to perform this type of test. For example:

 

 

if(new Set<Integer>{1,2,3}.contains(x)) { // this is equal to if(x==1||x==2||x==3) }

The idea is that you first create an anonymous Set, pre-populated with the conditions to check for, then use the contains instance method to compare a value you are looking for. This trivial example is actually longer than using the original code, but if you had a set of 50 values (a list of states in the United States, for example), this construct would be far more efficient. Strings are particularly efficient using this method:

 

 

if(new set<string>('red;green;blue;yellow;white;black;orange' .split(';') .contains(favoriteUserColor)) { // Same as // if( favoriteUserColor=='red' || favoriteUserColor=='green' || favoriteUserColor=='blue' || favoriteUserColor=='yellow' || favoriteUserColor=='white' || favoriteUserColor=='black' || favoriteUserColor=='orange') }

 

 As you can see, the larger the conditional statement, the more efficient the string comparison becomes.

 

 

This was selected as the best answer
PamSalesforcePamSalesforce
Thanks a lot..I tried it and it works well.