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

Multidimensional maps

I'm creating a trigger to do some validations and I have a map that is Country_Key__c, Description_Name__c, Name.  I would like to get them all into a map so I can do some validations.  I have googled and search this form for some answers, but I am very new to Salesforce and Apex and do not grasp many of the concepts..  I would like to create these maps to do be multidimensional..

trigger OverrideAddressTrigger on Sampling__c (before insert) {

    Map<String, Validation_Country__c> validCountries = new Map<String, Validation_Country__c>();
    Map<String, Validation_Region__c> validRegions = new Map<String, Validation_Region__c>();
    //Getting Country Validation.
    For(Validation_Country__c obj : [Select Id,Country_Name__c,Transportation_Zone__C FROM Validation_Country__c]){
        validCountries.put( obj.Country_Name__c,obj);
    //Getting Region Validation
    For(Validation_Region__c objR : [Select Id,Country_Key__c,Description__c,Name FROM Validation_Region__c]){
        validRegions.put( objR.Description__c,objR);
        validRegions.put( objR.Country_Key__c,objR);
    For( Sampling__c s :{
        IF(s.Country__c != null){
            IF(validRegions.containsKey(s.Country__c) && (validRegions.contains(s.State_Province__c)){//Not quite right.. logic? 
                s.Country__c == validCountries.get(s.Country__c);//Wrong needs fixing

     I did not understand what you mean by multi dimensional maps. From the code above  the 'not quite right..logic' line says validRegions.contains()  maps do not have a 'contains' function..also when you are populating the validRegions map there is nothing called State_province in it..does the Validation Region object has any field called State Province or does the State_Province__c  from Sampling__c match with any other field in Valid_Region__c object? 

In the last line you can do a comparison like this :
if(s.Country__c == validCountries.get(s.Country__c).Country_Name__c)
 I have 2 Objects that have data that I need to validate to Sample__c.  Validation_Region__c has Country_Key__c, Description__c(State/province info), and Name.   Validation_Country__c has Transportation_Zone__c that i'm going to try and autopopulate off of Country_Key__c.  

I thought maps had a contain function.  I will also post the requirements for this, I'm very new to Apex programming and I was pushed this assignment at the last second, and appreciate any guidance you veterans can tell me.  Also am I thinking of the logic properly?

This support is needed on the Sampling__c object.
When saved with field Override__c = "Yes", we need to verify
1. that values entered in the Country__c field are are restricted to those listed on the attached file on the first tab in the Valid Country Codes column (column A)
2. that values in the State_Province__c field are restricted to those listed as valid for the entered Country__c value as noted on the second tab (column C lists valid options for the Country Code listed in column A)
3. that values entered in the Zip_Postal_Code__c field adhere to the attached rules for the entered Country__c as noted on the first tab (columns D, E and F indicate the rule for the Country Code listed in column A)
4. that values entered in the SAP_Transportation_Zone__c field are restricted to those listed as valid for the entered Country__c value as noted on the first tab (column c) – in fact if your trigger instead could auto-populate this field based on what is valid for the country code the user selects in the  Country__c field, that would be even better!
Also here is a update to my code.. Thanks for any assistance

trigger OverrideAddressTrigger on Sampling__c (before insert) {

// Top level map is keyed by Country. Inner Map is keyed by Region
Map<String, Map<String, Validation_Region__c>> validRegions = new Map<String, Map<String, Validation_Region__c>>();
// ...
for(Validation_Region__c objR : [Select Id,Country_Key__c,Description__c,Name FROM Validation_Region__c]){

    string countryKey = objR.Country_Key__c;

    Map<String, Validation_Region__c> regionMap = validRegions.get(countryKey);
    // Maybe rework to use Map.containsKey rather than null check. Would be cleaner.
    if(regionMap == null) {
        regionMap = new Map<String, Validation_Region__c>();
        validRegions.put(countryKey, regionMap);
    string regionKey = objR.Description__c;
    validRegions.put( regionKey,objR);

for( Sampling__c s :{
    if((s.Country__c != null) && (S.Override__c == 'Yes')){
        string countryKey = s.Country__c;
        string regionKey = s.State_Province__c;

        if(validCountries.containsKey(countryKey) && validRegions.containsKey(countryKey)) {
            // The country appears to be valid and there are possible Region matches
            Validation_Country__c vc = validCountries.get(countryKey);

            if(validRegions.get(countryKey).containsKey(regionKey)) {
                // The Region belongs to the country
                Validation_Region__c vr = validRegions.get(countryKey).get(regionKey);