function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kellan ScheiberKellan Scheiber 

I have the below where I am trying to map an String to an Time schedule and am stuck at where I am unable to get the variable to assign to the map, I am getting an error where the key is not assigning. Please see class below

public class BusSchedule {
	

    public Time [] schedule1;
    public String busName;
    public Map<String, Time[]> schedule2 = new Map<String, Time[]>();
    
    public void putSchedule(String busLine, Time [] schedule){
        
        busName = busLine;
        schedule2.put(busName, schedule);
        schedule1 = schedule2.get(busName);
             
        
    }

    public Time[] getSchedule(String busLine){
        Time[] bSched = new Time[]{};
        busName = busLine;
            if (busLine != null && schedule2.containsKey(busName)) {
            bSched = schedule1;
        } else {
            bSched.add(Time.newInstance(11,0,0,0));
            bSched.add(Time.newInstance(21,0,0,0));
       }
        
        return bSched;
    }
}

 
Alain CabonAlain Cabon
Hi,
public class BusSchedule {
	

    public Time [] schedule1;
    public String busName;
    public Map<String, Time[]> schedule2 = new Map<String, Time[]>();
    
    public void putSchedule(String busLine, Time [] schedule){
        
        busName = busLine;
        schedule2.put(busName, schedule);
        schedule1 = schedule2.get(busName);
             
        
    }

    public Time[] getSchedule(String busLine){
        Time[] bSched = new Time[]{};
        busName = busLine;
        if (busLine != null && schedule2.containsKey(busName)) {
            bSched = (Time[]) schedule2.get(busName);
        } else {
            bSched.add(Time.newInstance(11,0,0,0));
            bSched.add(Time.newInstance(21,0,0,0));
       }
        
        return bSched;
    }
}
 
BusSchedule bus = new BusSchedule();
Time[] bSched = new Time[]{};
bSched.add(Time.newInstance(11,0,0,0));
bSched.add(Time.newInstance(21,0,0,0));
bus.putSchedule('BUS001',bSched);

Time[] bSched2 =(Time[]) bus.getSchedule('BUS001');
system.debug('bSched2.size : ' + bSched2.size());
system.debug('bSched2 #1: ' + bSched2[0]);
system.debug('bSched2 #2: ' + bSched2[1]);

Regards,
Alain

 
Kellan ScheiberKellan Scheiber
Thank you Alain! You sir are a genius. I had the class correct but I was passing in the variable instead of an String and that was the issue.