How can I iterate and grab the data for different month instead of passing month and execute the soql and return the data and I believe this code can be written better than what I'm doing here...

Here is my method that executes SOQL and pass the month and it returns the data based on the month:

public List<WrapperEmployeeTime> loadData(string month)
    List<Employee__c> soqlData = [SELECT id,name,..... from Employee__c where month__c =: month];

    List<Employee__c> loadempTime = new List<Employee__c>();
    for(Employee__c e : soqlData)
       WrapperEmployeeTime b = new WrapperEmployeeTime();
       //do good stuffs here..
    return loadempTime;

Calling method:

public List<WrapperEmployeeTime> wrapEmployeeJanList {get;set;}
wrapEmployeeJanList = loadData('January');

public List<WrapperEmployeeTime> wrapEmployeeFebList {get;set;}
wrapEmployeeFebList = loadData('Febuary');

public List<WrapperEmployeeTime> wrapEmployeeMarList {get;set;}
wrapEmployeeMarList = loadData('March');

all the way upto 12 months....

I could pull all the data of 12 months and loop through based on the month but I'm not sure what is the best way of achieving this.

1 Answer 1


You can create Map with month and list of employees:

    Map<String, List<Employee__c>> monthToEmployeeMap = new Map<String, List<Employee__c>>{
    'January' => new List<Employee__c>(),
    'February' => new List<Employee__c>(),

for (Employee__c  empl : [SELECT Id,... WHERE Month__c in: monthToEmployeeMap.keySet()]) {
  • compiling error: expecting a right parentheses found '==>'
    – Nick
    Jun 16, 2016 at 21:33
  • change () to {} for map, and not '==>', just '=>', you can also fill map with standard method someMap.put('key', 'value');
    – Bogdan K
    Jun 16, 2016 at 21:45
  • 1+ great thank you, do i need to loop 12 times in order to load the data for each month (wrapEmployeeJANList, wrapEmployeeFEBList ...) ?, in this case i'm loading the data specific to the month wrapEmployeeJanList
    – Nick
    Jun 16, 2016 at 22:57
  • You will get all data in one for loop, i dont know the real scenario, why you need 12 separate lists, but anyway you can use this map on visualforce page and iterate through lists for particular month with using a key.
    – Bogdan K
    Jun 17, 2016 at 7:43

You must log in to answer this question.

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