+ Start a Discussion
ilewi121ilewi121 

Reference static list in method


I'm trying to prevent multiple SOQL queries in my method by creating a static list outside of the method.
The list is pulling from custom settings, so they should be static when the class runs.
Unfortunately, when I run the test for this class, the debug log shows nothing.
Am I putting this list in the wrong place?

Class
public class modeRanges{

    public static List<ModeRange__c> ModeRanges = [SELECT Mode_Type__c, Spend_Picklist_Value__c, Volume_Picklist_Value__c FROM ModeRange__c];

    public static void setSpendRangeFromVolumeRange(List<Mode__c> Modes){
	System.debug('ModeRanges: ' + ModeRanges);
        for(Mode__c m :Modes){
            for(ModeRange__c r: ModeRanges){
                if(m.Type__c == r.Mode_Type__c && m.Est_Volume__c == r.Volume_Picklist_Value__c){
                    m.Est_Spend__c = r.Spend_Picklist_Value__c;
                }
            }
        }
    }    
}

Debug Log
15:45:01:042 USER_DEBUG [12]|DEBUG|ModeRanges: ()

Best Answer chosen by ilewi121
Stephan SpiegelStephan Spiegel
Does your debug log have an entry for the SOQL query (SOQL_EXECUTE_BEGIN/SOQL_EXECUTE_END)?

If yes, how many rows is it returning?

My best guess is that your test method setup doesn't create any ModeRanges__c records. Test methods don't have access to custom settings by default. Either add SeeAllData=true to your @isTest annotation, or create some ModeRanges__c settings in the setup of your test.

All Answers

Stephan SpiegelStephan Spiegel
Does your debug log have an entry for the SOQL query (SOQL_EXECUTE_BEGIN/SOQL_EXECUTE_END)?

If yes, how many rows is it returning?

My best guess is that your test method setup doesn't create any ModeRanges__c records. Test methods don't have access to custom settings by default. Either add SeeAllData=true to your @isTest annotation, or create some ModeRanges__c settings in the setup of your test.
This was selected as the best answer
ilewi121ilewi121
True, I did not create any ModeRanges__c in my test. I will add that and retest.

That said, I tested via UI and it still doesn't work, even though I have all the necessary ModeRanges__c records. When I run my SOQL query in the query editor, it pulls the records, so my methods are not able to access the list in the outer class.

Do you have any ideas how I can access a static list in the outer class from a method within the class?
ilewi121ilewi121
Well, adding (SeeAllData=True) fixed it! Thanks for your help!