Search code examples
salesforceapex-codevisualforce

Can i have a list with Multiple datatypes in salesforce


I have a string List time1 with values (00:00 AM,00:30 AM, 01:00 AM, 01:30 AM ........... so on till 11:30 PM)

I also have a list appList of a custom object appointment__c.

This list hold only records of the set appointments

ie if a appointment is set for 8 AM - 8:30 AMand 10AM - 11:00AM then it would hold only these 2 records

I need to create a grid or table to show the appointments for the day from 00:00 AM to 11:30 PM.

I would need to read thru each row in time1 and check if there is a corresponding match to that time in appList, if its found then i need to show the details from appList else it should display as free against the time. I would also need to store it in a list so that i can use it in the VF page. How would i have to define this list? Can i have the list store the time in one column and have list of appointment object in other column

Any better way of approaching this?


Solution

  • In this case I'd use a class and have a list of objects for that class:

    class CTimeSlot
    {
        public Time           tStart         {get; set;}
        public Appointment__c sAppointment   {get; set;}
    
        public CTimeSlot(Time startTime)
        {
            tStart = startTime;
            Appointment__c = null;
        }
    }
    
    // ** snip ** 
    
    list<CTimeSlot> liTimeSlots = new list<CTimeSlot>();
    
    // ** snip ** loop through times, and for each add an entry to the list
    
        CTimeSlot newSlot = new CTimeSlot(loopTime);
        liTimeSlots.add(newSlot);
        mapTimeToSlot.put(loopTime + '', newSlot);
    }
    
    // ** snip ** when running through your query results of Appointment__c objects:
    for(Appointment__c sAppointment : [select Id, Time__c from Appointment__c where ...])
    {
        if(mapTimeToSlot.get(sAppointment.Time__c) != null)
        {
            mapTimeToSlot.get(sAppointment.Time__c).sAppointment = sAppointment;
        }
    }
    

    You can then fill this list with instances of CTimeSlot, and for times where you have an appointment set it to sAppointment on the instance — this could be made easier by having a map of the slots as well, mapping a time (as a string) to a CTimeSlot.

    In the page you could then just repeat over the list:

    <table>
    <apex:repeat var="slot" value="{!liTimeSlots}">
        <tr>
            <td><apex:outputText value="{!slot.tStart}"/></td>
            <td>
                <apex:outputText value="{!IF(ISNULL(slot.sAppointment), 'Free', slot.sAppointment.SomeField)}"/>
            </td>
        </tr>
    </apex:repeat>
    

    Hopefully this will give you some ideas and set you off on the right path!