2

Some quick background of what I'm trying to do..

The following is my Apex Trigger. It is a validation Trigger on Sampling_c which will validate that Country_c is a Valid country using Validation_country_c. When it validates Country_c it will look into State_province_c and will then validate that it is actually a state So .. If Country_c = US and State_province_c = CA then we are okay. But IF Country_c = US and State_province_c = ZZ221(whatever) it should fail. Now these condition checks only triggers when Override_c = 'Yes'.

Description__c holds my State_Province__c validation info

So this is where I am at so far.
I was able to return all of my System.debugs so the conditions are right besides this one which has my uncovered line of code.

if(validRegions.get(countryKey) != null && validRegions.get(countryKey).get(regionKey) != null) {
 Validation_Region__c vr= validRegions.get(countryKey).get(regionKey);
            System.debug('This is VR'+vr); //This doesn't return

I believe it is because regionKey is not being filled properly here.

For(Validation_Region__c objR : [Select Id,Country_Key__c,Description__c,Name FROM Validation_Region__c]){
String countryKey = objR.Country_Key__c;
System.debug('System Debug!!!!' + 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) {
    System.debug('Inside regionMap == Null condition');
    regionMap = new Map<String, Validation_Region__c>();
    validRegions.put(countryKey, regionMap);
    System.debug('This is Broken' +validRegions);
}
string regionKey = objR.Description__c; 
regionMap.put( regionKey,objR);
} 

With the help of a few members here I have written my test class which I will post below, along with my full code. Thanks for all the help.. I know I've asked for a lot of it over the last couple days.

Test Class --

@IsTest(SeeAllData=false)
public class OverrideTest{

static testmethod void addressOverride(){

    Sampling__c s = new Sampling__c();

    Validation_Region__c region = new Validation_Region__c();
    region.Name = 'US';
    region.Country_Key__c = region.Name; 
    insert region;

s.Quantity_of_Samples__c = 2;
s.Override__c = 'Yes';
s.Country__c = 'US';
s.State_Province__c = 'California';
insert S;
}
}

Trigger--

trigger OverrideTrigger 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;
System.debug('System Debug!!!!' + 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) {
    System.debug('Inside regionMap == Null condition');
    regionMap = new Map<String, Validation_Region__c>();
    validRegions.put(countryKey, regionMap);
    System.debug('This is Broken' +validRegions);
}
string regionKey = objR.Description__c; 
regionMap.put( regionKey,objR);
}


For( Sampling__c s : Trigger.new){
If((s.Country__c != null) && (S.Override__c == 'Yes')){
    String countryKey = s.Country__c;
    String regionKey = s.State_Province__c;

    System.debug('This is the countryKey'+countryKey);
    System.debug('This is the regionKey' +regionKey);

        if(validRegions.get(countryKey) != null &&  validRegions.get(countryKey).get(regionKey) != null) {
            // The Region belongs to the country
            System.debug('This is the validRegions'+validRegions );
            Validation_Region__c vr= validRegions.get(countryKey).get(regionKey);
            System.debug('22345 This is VR'+vr);
        }else{
                System.debug('Breaking');
             break;
        }

    }
}
}
4
  • 1
    You should add a couple more debug statements to your trigger code; Debug out the values of validRegions.get(countryKey) and validRegions.get(countryKey).get(regionKey) before the if block and then you'll know which of your maps is missing expected data.
    – Mark Pond
    Feb 27, 2014 at 21:04
  • Can you show us how do you feed your map 'validRegions' ? What are the results your are getting ?
    – brovasi
    Feb 27, 2014 at 22:04
  • validRegions is created by Map<String, Map<String, Validation_Region__c>> validRegions = new Map<String, Map<String, Validation_Region__c>>(); and then in my validRegions.put(countryKey,regionMap); But I believe the logic is skewed. Right now my System.debug for it spits out US which is my input on my test class.
    – EricSSH
    Feb 27, 2014 at 22:44
  • Hi Mark, good call on that I will go ahead and look into that deeper now. 14:54:18.111 (6111359000)|USER_DEBUG|[31]|DEBUG|This is the countryKey{null=Validation_Region__c:{Name=US, Id=a1SL0000000kAEbMAM, Country_Key__c=US}} 14:54:18.111 (6111444000) |USER_DEBUG|[32]|DEBUG|This is the regionKeynull
    – EricSSH
    Feb 27, 2014 at 23:15

1 Answer 1

0

Thanks much for the help, I relized that I wasn't actually passing anything back with my Test class.. Here is what I updated with my test class. Thanks for the responses

Validation_Region__c region = new Validation_Region__c();
    region.Country_Key__c = 'US';
    region.Description__c = 'California';
    region.Country_Key__c = region.Country_Key__c;     
    insert region;
1
  • Little update to this. Just because this works doesn't mean its right, so what I mean by that is I'm "artificially" populating Validation_Region__c which I shouldn't be because I already have that information in that object so I need to figure out why that is happening.
    – EricSSH
    Feb 28, 2014 at 0:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .