This is likely easier than I am making it, but I cannot seem to resolve:

I have a table that stores schedule information for the days of the week. The fields are day number (1 - 7), Day name (Mon, Tues, etc), Start Time and End Time. For a given schedule, I might have the following items:

1     Monday     8:30 AM    12:30 PM
4     Thursday   12:30 PM   4:30 PM

In order to allow the user to edit the list, I need to present a list of these items AND add in empty items for days that do not have scheduled times. For example:

1     Monday     8:30 AM    12:30 PM
2     Tuesday    
3     Wednesday
4     Thursday   12:30 PM   4:30 PM    

Essentially, I need to pull the info into a list using SOQL, sort by day number and then if the data does not contain a specific day number (3), then I need to be able to add a blank object for 3 to my wrapper set. However, my two approaches were to either iterate through the day numbers or iterate through the data and neither seems to work without making multiple calls to the database. Can someone point me in the proper direction here? Or is the multiple calls (i.e., running a SOQL query for each day number) the only way to get it done?

UPDATING WITH CURRENT EXISTING CODE (Note: this shows how I go about retrieving existing data and separately shows how I create a "blank" week for input. My question is how to merge the two--show a week where days that a schedule has been added appear for editing as well as blank days where new items can be placed:

Wrapper class for schedule line items:
public class scheduleItem {
    public string id {get; set;}                                            
    public Schedule_Line_Item__c li {                       
            if(li == null) li = new Employment_Job_Wage_Schedule_Line_Item__c();
            return li;
    public boolean forUpdate {get; set;}                                    
    public boolean forDelete {get; set;}                                    

// List of scheduleItems for display and edit
public list<inputItem> fullSchedule {
        if(fullSchedule == null) fullSchedule = new list<inputItem>();
        return fullSchedule;

// SOQL Query that retrieves existing Schedule_Line_Item__c (scheduleItem.li)    
list<Schedule_Line_Item__c> mySchedDetail = new list< Schedule_Line_Item__c>();
mySchedDetail = [SELECT Id, Day_Number__c, Day_of_Week__c, End_Time__c, Hours_for_Day__c,
                             Job_Coach__c, Job_Coach_End_Time__c, Job_Coaching_Type__c, Job_Coach_Start_Time__c,
                             Lunch_and_Break_Time__c, Start_Time__c
                             FROM Schedule_Line_Item__c
                             WHERE Schedule_Id__c = : mySchedule[0].Id
                             ORDER BY Day_Number__c];

// Loops through existing Schedule_Line_Item__c and creates new scheduleItems
if(mySchedDetail.size() > 0){
                for(Schedule_Line_Item__c i : mySchedDetail){
                    scheduleItem sItem = new scheduleItem();
                    sItem.li = i;
                    sItem.forUpdate = false;
                    sItem.forDelete = false;
           // Code used elsewhere to create a "blank" week for input
           for (Integer x=1; x<8; x++) {
                scheduleItem ii = new scheduleItem();
                ii.li.Day_Number__c = x;
                ii.li.Day_of_Week__c = weekdays.get(x);
                ii.forDelete = false;

Thanks in advance, KMT

  • Does your schedule span multiple weeks or will you only deal with 1 week at a time? Could Monday have two entries in the table for the same date: for example: 8:30 - 12:30 and 2:30-5:00 Sep 23, 2015 at 18:26
  • no, Monday is only ever one entry and it is a weekly schedule (one week at a time).
    – KMT333
    Sep 23, 2015 at 18:54
  • Can you update your question and add the source code for your current solution. Sep 23, 2015 at 19:34
  • done. i have not created a solution yet, but I can show you the two methods I am trying to combine. I'm trying to sort out the best method to create the solution.
    – KMT333
    Sep 23, 2015 at 19:58

1 Answer 1


If a specific schedule Schedule_Id__c can only ever have 7 line item entries then you could do something similar to this:

  list<Schedule__Line_Item__c> schedList = [select Id, Day_Number__c,` Day_of_Week__c, End_Time__c, from Schedule__Line_Item__c where Schedule_Id__c = :mySchedule[0].Id];

  map<int, Schedule__Line_Item__c> weekMap = new Map<int, Schedule__Line_Item__c>();

  for (Schedule__Line_Item__c item: schedList){
     weekMap.put(item.Day_Number__c, item);

  for (Integer j = 1; j<= 7; j++){
     if (! weekMap.containsKey(j)){
        Schedule__c missingDay = getScheduleLineItemForDay(j));
        weekMap.put(j, missingDay);   
  • DW--the general logic here worked slick! thanks for the assist.
    – KMT333
    Sep 23, 2015 at 23:50

You must log in to answer this question.

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