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
plapla 

If else structure

Hello,

 

Is there an easier way to structure where state = CA, OR, WA, ID, NV, AZ then Region = West ?

if state = ND, SD, NE, KS, OK, TX then Region = Central

 

I know in MS SQL by using the IN statement. For example:  In (ND, SD, NE, KS, OK, TX). However is there an equivalent coding structure in Apex code? Please advise.

 

Thanks

Paul

 

Best Answer chosen by Admin (Salesforce Developers) 
Val ValinoVal Valino

You are welcome, can you please mark my answer as solved?

All Answers

Val ValinoVal Valino

Put them in a set or put them in a Map.

 

For example have a set just for the West region.

 

Set<string> WestRegion = new Set<string>(list all states);

 

then if(WestRegion.contain(string)){

variable = 'West';

}

 

Another way is maybe by using a map.

 

Map<string, string> regionMap = new Map<string, string>();

 

then add all states by doing this the following:

 

regionMap.put('TX', 'Central');

regionMap.put('OK', 'Central');

 

variable = regionMap.get(state);

Kiran  KurellaKiran Kurella

Try the following static method:

 

You can call the method as follows:

 

containsIgnoreCase(State, new list<String> {'CA', 'OR'}), 

 

 

public static Boolean containsIgnoreCase(String compString, list<String> compList) {

 if (compString != null && compList != null && !compList.isEmpty()) {
    for (String s : compList) {
          if (compString.containsIgnoreCase(s)) return true;
    }
 }

 return false;
}

 

plapla

Hello,

Thanks for your tips. I followed your instructions but encounterred an issue saving the trigger. The error says "expecting a right parentheses, found ',' " at the first statement of the Set string below. Please advise

 

 

trigger AccountRegion on Account (before update) {

  String State;
  String Region;
 
  Set<string> WestRegion = new Set<string>('WA', 'OR', 'CA', 'ID', 'NV', 'AZ', 'UT', 'MT', 'WY', 'CO', 'NM');
  Set<string> CentralRegion = new Set<string>('ND', 'SD', 'NE', 'KS', 'OK', 'TX');

 
   for(Account AccountUpdated:trigger.new){
      
       State = AccountUpdated.Situs_State__c;
      
       If (State != null){
      
          if(WestRegion.contain(State)){
                Region = 'West';
          } else if (CentralRegion.contain(State)){
               Region = 'Central';
          }

        }
     
        AccountUpdated.Region__c = Region;
           
    }  //end for
       
 }

 

Val ValinoVal Valino

Can you post the whole error? Which line exactly is causing the error? Does it say?

Val ValinoVal Valino
trigger AccountRegion on Account (before update) {

  String State;
  String Region;
 
  Set<string> WestRegion = new Set<string>{'WA', 'OR', 'CA', 'ID', 'NV', 'AZ', 'UT', 'MT', 'WY', 'CO', 'NM'};
  Set<string> CentralRegion = new Set<string>{'ND', 'SD', 'NE', 'KS', 'OK', 'TX'};

 
   for(Account AccountUpdated:trigger.new){
      
       State = AccountUpdated.Situs_State__c;
      
       If (State != null){
      
          if(WestRegion.contain(State)){
                Region = 'West';
          } else if (CentralRegion.contain(State)){
               Region = 'Central';
          }

        }
     
        AccountUpdated.Region__c = Region;
           
    }  //end for
       
 }

 Sorry use curly braces like this above

plapla

Thanks so much. It works fine now.

 

Have a nice day.

Paul

 

Val ValinoVal Valino

You are welcome, can you please mark my answer as solved?

This was selected as the best answer