I have a formula in Excel which I'd like to replicate in an Apex Class. The below formula is in cell E3
.
=INDEX(Sheet2!$B$2:$F$4,MATCH('Sheet1 (2)'!B3,Sheet2!$B$3:$B$4,0)+1,MATCH('Sheet1 (2)'!D3,Sheet2!$C$2:$F$2,0)+1)
which retrieves values from the below matrix in Sheet2
The first MATCH()
retrieves the location of the matching Country (row) and the second retrieves the location of the matching Region (column). Index then returns the value in the cell based on the combination of the row & column locations.
I'll retrieve the Country & Settlement Region values earlier in my Class which I can obviously use as my keys.
I know that I could store a list of Countries in one Map, along with an string / integer, do the same for Settlement Regions and then us If
statements to return a different value for each possible combination of Countries and Settlement Regions.
The countryRef
& regionRef
values will actually be stored in a Custom Setting but I've populated the Maps below, to keep the example as simple as possible
String country;
String region;
Map<String,String> countryRef = new Map<String,String>();
Map<String,String> regionRef = new Map<String,String>();
Map<Id,Integer> records = new Map<Id,Integer>();
country.put('UK','a');
country.put('US','b');
region.put('UK','a');
region.put('US','b');
region.put('EU','c');
region.put('Outside EU','d');
Integer rtndCountry = country.get('UK');
Integer rtndRegion = region.get('Outside EU');
if (rtndCountry = 'a' && rtndRegion = 'd'){
records.put(Id,4);
} else if (//etc.){
}
But using the If
statements to manage the different country & region combinations seems inefficient and awkward to maintain, is there an alternative Apex Class (if that's the correct term) which I could use in place of Excel's INDEX(
instead?
Is there an alternative approach that I could adopt?