Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

The following is a snippet of my code that many of you guys helped me with. Starting at the for loop it is failing to get any code coverage. I'm very new to the Apex programming scene, and I an unable to debug myself many of the issues. So my question is what would cause String countryKey = objR.Country_Key__c; to fail? I believe it is because the For loop is not actually populating my data, but I know my query is correct because it runs in Force.com Explorer. I will also post my test class.

Side note to responses, please break it down barney style so I can attempt to understand the problem at hand.

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) {
    regionMap = new Map<String, Validation_Region__c>();
    validRegions.put(countryKey, regionMap);
  }

  string regionKey = objR.Description__c; 
  regionMap.put( regionKey,objR);
}

Test Class

@IsTest
public class OverrideTest{

  static testmethod void addressOverride(){

    Sampling__c s = new Sampling__c();

    s.Quantity_of_Samples__c = 2;
    s.Override__c = 'Yes';
    s.Country__c = 'US';
    s.State_Province__c = 'CA';
    //s.Override_Default_Ship_Method__c = True;

    insert S;
  }
}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

Simply put, if you are not getting any code coverage within your loop, it's because you are not pulling back any records from your database. Within your test class I would also create a record for your Validation_Region__c table. Please try that and see if it resolves your issue.

On a side note, I prefer to use the annotation @isTest with the parameter SeeAllData=false. It is not required but it makes for better code coverage and testing since you would be testing all of your scenarios from scratch.

please try something along these lines and tell me your result:

public class OverrideTest{

    @IsTest(SeeAllData=false)
    static void addressOverride(){
        Validation_Region__c region = new Validation_Region__c();
        region.Name = 'US';
        region.Country_Key__c = region.Name; 
        insert region;

        Sampling__c s = new Sampling__c();
        s.Quantity_of_Samples__c = 2;
        s.Override__c = 'Yes';
        s.Country__c = 'US';
        s.State_Province__c = 'CA';
        insert S;
    }
}

Also please take a look at your method, you seem to be losing information you are pulling back out. Please try this:

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]){
    // get outer map key
    String countryKey = objR.Country_Key__c;
    // create new instance
    Map<String, Validation_Region__c> regionMap = new Map<String, Validation_Region__c>();
    // check to see if value exists
    if(validRegion.containsKey(countryKey)){
        // if we have existing value, pull it back out so we don't lose data
        regionMap = validRegion.get(countryKey);
    }

    // create internal map key
    string regionKey = objR.Description__c; 
    // populate map
    regionMap.put( regionKey,objR);
    // repopulate our entire map so we dont lose data
    validRegions.put(countryKey, regionMap);
}
share|improve this answer
    
Alright!! Awesome so it came back as 97% code coverage, there is only line that isn't getting covered now. I will do some more analysis before I reach out to you guys. Thanks a lot –  EricSSH Feb 27 at 18:16
    
I Double A I removed the answer flag, because I think I understand what is going on more so now. Yes it is working now because I artificially pushed it in. But in reality I need all of that information to be queried from my object. So can you take a look at my For loop and see if there are any issues with it? –  EricSSH Feb 27 at 23:59
    
I'm not sure what you are asking for. I've achieved what you've asked originally... if you are wondering why data is missing when you test this out yourself. This may be because you may be overrideing your map... IE: If you have 2 USA values as your description you'll override your first value once you loop over to your second value. Other than that, I'll need more information. There's not enough to resolve what ever issue you are facing now –  Double A Feb 28 at 0:12
    
I'm wondering why objR is not being populated with my Validation_Region__c object. Validation_Region__c has all of the validation data so I shouldn't need to put that information in my test class. There is something wrong with that For loop I believe and I don't know what it is. –  EricSSH Feb 28 at 0:31
    
If the test class works... are you sure you have records within that table? Make sure you're in the same Org Instance too –  Double A Feb 28 at 0:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.