+ Start a Discussion
Lehel GyeresiLehel Gyeresi 

Create an Apex class that writes and reads from the org cache Trailhead Challenge error

Hello,

I am trying to complete the challenge for the Use Org & Session Cache module on Salesforce Trailhead. Here are the instructions:

In this challenge, you’ll write an Apex class that writes and reads a bus schedule from the org cache. Your method for reading the cache has to handle cache misses.
  • Create a partition called BusSchedule with 0 MB for the size of org cache and session cache. The 0 MB allocation enables you to test cache misses.
  • Create a public Apex class called BusScheduleCache.
  • Add this variable to the class: private Cache.OrgPartition part;
  • In the constructor, create a new instance of Cache.OrgPartition by passing it the partition name (local.BusSchedule). Assign this object to the class variable (part).
  • Add two public methods. a. The first method, putSchedule(), returns void and takes these parameters: String busLine, Time[] schedule. b. The second method, getSchedule(), returns a bus schedule as a time array (Time[]) and takes this parameter: String busLine.
  • Implement the putSchedule() method so that it stores the passed-in values in the org cache by using the partition class variable (part).
  • Implement the getSchedule() method so that it returns the schedule for the specified bus line by using the partition class variable (part).
  • Add logic to the getSchedule() method to handle cache misses. If null is returned for the cached value, getSchedule() should return the following default schedule as a Time array with two Time objects: one Time object value of 8am and another of 5pm. Use the Apex Time.newInstance() method to create the Time objects.
Here is my current code:
 
public class BusScheduleCache {   
    // Get partition
    private Cache.OrgPartition part;
    String partitionName = 'local.BusSchedule';
    
    public BusScheduleCache(String partitionName) {
		Cache.OrgPartition newpart = Cache.Org.getPartition(partitionName);
        part = newpart;
    }
    
    public static void putSchedule(String busLine, Time[] schedule) {
        Cache.Org.put(busline, schedule);
    }
        
    public static Time[] getSchedule(String busLine) {
        Time[] schedule;
        
        // Get a cached value
		Object obj = Cache.Org.get(busLine);
		// Cast return value to a specific data type
		Time t2 = (Time)obj;
        if (t2 != null) {
        	schedule.add(t2);
        }
        else {
            Time t3 = Time.newInstance(8,0,0,0);
            schedule.add(t3);
            Time t4 = Time.newInstance(17,0,0,0);
            schedule.add(t4);
        }        
        return schedule;
    }  
      
}



Here is the error I am receiving:

Challenge Not yet complete... here's what's wrong: 
The Apex Class BusScheduleCache is not properly implemented. Please follow the requirements and ensure everything is setup correctly

Can someone please help?
Best Answer chosen by Lehel Gyeresi
Lehel GyeresiLehel Gyeresi
Eventually I passed with this code:
 
public class BusScheduleCache {   
    // Get partition
    private Cache.OrgPartition part;
    String partitionName = 'local.BusSchedule';
    
    public BusScheduleCache () {
        if(partitionName !=null){
            Cache.OrgPartition orgPart = new Cache.OrgPartition(partitionName);
        	if(orgPart != null){
        		part = orgPart;
        	}
        }
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        if (part != null){
            part.put(busline, schedule);
        }
    }
        
    public Time[] getSchedule(String busLine) {
        Time[] schedule = new List<Time>();
        
        // Get a cached value
		Object obj = part.get(busLine);
		// Cast return value to a specific data type
        Time t2;
        if (obj != null) {
        	t2 = (Time)obj;
        }
        if (t2 != null) {
        	schedule.add(t2);
        }
        else {
            Time t3 = Time.newInstance(8,0,0,0);
            if (t3 != null){
            	schedule.add(t3);
            }
            Time t4 = Time.newInstance(17,0,0,0);
            if (t4 != null){
                schedule.add(t4);
            }
        }     
        return schedule;
    } 
      
}

 

All Answers

Lehel GyeresiLehel Gyeresi
I have updated the code to the following, but still no luck:
 
public class BusScheduleCache {   
    // Get partition
    private Cache.OrgPartition part;
    String partitionName = 'local.BusSchedule';
    
    public BusScheduleCache (String partitionName) {
		Cache.OrgPartition newpart = new Cache.OrgPartition(partitionName);
        part = newpart;
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        part.put(busline, schedule);
    }
        
    public Time[] getSchedule(String busLine) {
        Time[] schedule;
        
        // Get a cached value
		Object obj = part.get(busLine);
		// Cast return value to a specific data type
		Time t2 = (Time)obj;
        if (t2 != null) {
        	schedule.add(t2);
        }
        else {
            Time t3 = Time.newInstance(8,0,0,0);
            schedule.add(t3);
            Time t4 = Time.newInstance(17,0,0,0);
            schedule.add(t4);
        }        
        return schedule;
    }  
      
}

 
Tolga SunarTolga Sunar
Hey,

I cannot seem to pass this challenge as well. It feels like I'm missing something obvious.

My code is similiar to yours, with the difference being at getSchedule method:
 
public Time[] getSchedule(String busLine) {
    Object schedule = part.get(busLine);
    if (schedule==null) {
        return new List<Time>{Time.newInstance(08,0,0,0), Time.newInstance(15,0,0,0)};
            }
    else {
        return (Time[])part.get(busLine);
    }
}

If there's no confirmed bug in the challenge, I'd like to receive a hint (not the whole passing code).

Thanks in advance.
 
Lehel GyeresiLehel Gyeresi
Eventually I passed with this code:
 
public class BusScheduleCache {   
    // Get partition
    private Cache.OrgPartition part;
    String partitionName = 'local.BusSchedule';
    
    public BusScheduleCache () {
        if(partitionName !=null){
            Cache.OrgPartition orgPart = new Cache.OrgPartition(partitionName);
        	if(orgPart != null){
        		part = orgPart;
        	}
        }
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        if (part != null){
            part.put(busline, schedule);
        }
    }
        
    public Time[] getSchedule(String busLine) {
        Time[] schedule = new List<Time>();
        
        // Get a cached value
		Object obj = part.get(busLine);
		// Cast return value to a specific data type
        Time t2;
        if (obj != null) {
        	t2 = (Time)obj;
        }
        if (t2 != null) {
        	schedule.add(t2);
        }
        else {
            Time t3 = Time.newInstance(8,0,0,0);
            if (t3 != null){
            	schedule.add(t3);
            }
            Time t4 = Time.newInstance(17,0,0,0);
            if (t4 != null){
                schedule.add(t4);
            }
        }     
        return schedule;
    } 
      
}

 
This was selected as the best answer
Jeremy Shipley 13Jeremy Shipley 13
I can't get this to pass either. I even just copied and pasted the code marked as the best answer and still no luck.. any one else get this to pass?
 
Tolga SunarTolga Sunar
I was able to pass with the code marked as the best answer. Maybe you did a typo while defining the partition in your cache, better have a look outside the code.
Jeremy Shipley 13Jeremy Shipley 13
I actually closed out of trailhead and logged back in and then it passed..  Grrrrrr!
George WilliamsGeorge Williams
This can be accomplished with less code:
 
public class BusScheduleCache {
	private Cache.OrgPartition part;
    
    public BusScheduleCache(){
        Cache.OrgPartition initPart = new Cache.OrgPartition('local.BusSchedule');
        
        if (initPart != null){
            part = initPart;
        }
    }
    
    public void putSchedule(string busLine, Time[] schedule){
        if (part != null){
            part.put(busLine,schedule);
        }
    }
    
    public Time[] getSchedule(string busLine){
        Time[] bSched = new Time[]{};
        
        if (part != null && (Time[])part.get(busLine) != null){
        	bSched = (Time[])part.get(busLine);        
        } else{
            bSched.add(Time.newInstance(8,0,0,0));
            bSched.add(Time.newInstance(17,0,0,0));
        }
        
        return bSched;
    }
}

 
George WilliamsGeorge Williams
Tolga, your second time param is for 3PM, not 5PM.  Not sure if 08 is a valid pass for 8AM either but it probably is.  Other then that you are returning it as a list but if it compiled then Salesforce must not adhear to strict typing between lists and arrays.  I would suggest, though, that you change that return to a Time[] instead of a list.
Becca MorganBecca Morgan
#George Williams, Thank you!  I was sooo stuck on the schedule filling! I love the easy way and I made it way too hard (as is usuallly the case when I get stuck.)  I now have a delicious squirrel for dinner.
Ghanshyam Kumar 9Ghanshyam Kumar 9
This is the very simple and basic code to complete the challenge. Read it carefully.
public class BusScheduleCache{
    private Cache.OrgPartition part;
        
    public BusScheduleCache(){
        part = new Cache.OrgPartition('local.BusSchedule');
    }
    
    public void putSchedule(String busLine, Time[] schedule){
        if(part != null){
            part.put('busLine', schedule);
        }
    }
    
    public Time[] getSchedule(String busLine){
        Time[] schedule = (Time[])part.get(busLine);
        if (schedule != null) {
            return schedule;  
        } else {
            return new List<Time>{Time.newInstance(8, 0, 0, 0), Time.newInstance(17, 0, 0, 0)};
        }
    }
}
Mark Tyler CrawfordMark Tyler Crawford
The code in the answer to this question is overly complicated. See what Ghanshyam Kumar 9 did, it is the most elegant solution.
Jekabs AizpurvsJekabs Aizpurvs
A beter practice would be to replace the 
part = new Cache.OrgPartition('local.BusSchedule');
with
part = Cache.Org.getPartition('local.BusSchedule');

right? We already create a Cache partition through salesforce Classic earlier on.
ismael GACHAismael GACHA
public class BusScheduleCache {

    private Cache.OrgPartition part;
    
    public BusScheduleCache(){
        this.part = new Cache.OrgPartition('local.BusSchedule');
    } 
    
    public void putSchedule(String busLine, Time[] schedule){
        part.put(busLine, schedule);
    }
    
    public Time[] getSchedule( String busLine){
        Time sch = (Time) part.get(busLine);
        Time[] schedule = new List<Time>();
        if( sch != null)
            schedule.add(sch);
        else{
            Time t1 = Time.newInstance(8,0,0,0);
            Time t2 = Time.newInstance(17,0,0,0);
			schedule.add(t1);
            schedule.add(t2);
        }
        return schedule;
    }
}

 
Pavlo ShchurPavlo Shchur
Hy everyone, these few lines of code worked for me.
public class BusScheduleCache {
    private Cache.OrgPartition part;
    public BusScheduleCache(){this.part = Cache.Org.getPartition('local.BusSchedule');}
    
    public void putSchedule(String busLine, Time[] schedule){part.put(busLine, schedule);}   
    
    public Time[] getSchedule(String busLine){
        Time[] result = new Time[]{};
        return (part.get(busLine) != null) ? (Time[]) part.get(busLine) : (Time[]) new Time[]{Time.newInstance(8, 0, 0, 0), Time.newInstance(17, 0, 0, 0)};
    }
}
JamesDotyJamesDoty
public class BusScheduleCache {
    
    private Cache.OrgPartition part;
    String partition = 'local.BusSchedule';
    
    // custom constructor
    public BusScheduleCache() {
        Cache.OrgPartition orgPart = new Cache.OrgPartition(partition);
        if(orgPart != null) {
            part = orgPart;
        }
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        // store values in the org cache using 'part'
        if(part != null) {
            part.put(busLine, schedule);
        }
    }
    
    public Time[] getSchedule(String busLine) {
        // return schedule for the specified bus line using 'part'
        Time[] schedule = new List<Time>();
        
		Object obj = part.get(busLine);
        if(obj != null) {
            // return the value from the org cache if exists
           	schedule.add((Time)obj);
        } else {
        	// logic to handle cache misses
        	schedule = new Time[]{Time.newInstance(8,0,0,0), Time.newInstance(17,0,0,0)};	     
        }
        return schedule;
    }
}

This solution also works, Confirming for 2018
anshul saini 7anshul saini 7
Use this working Code(100%)
public class BusScheduleCache {
    private Cache.OrgPartition part;
    public BusScheduleCache(){this.part = Cache.Org.getPartition('local.BusSchedule');}
    
    public void putSchedule(String busLine, Time[] schedule)
    {
        part.put(busLine, schedule);
    }   
    
    public Time[] getSchedule(String busLine)
    {
        Time[] result = new Time[]{};
        return (part.get(busLine) != null) ? (Time[]) part.get(busLine) : (Time[]) new Time[]{Time.newInstance(8, 0, 0, 0), Time.newInstance(17, 0, 0, 0)};
    }
}

 
Devender Baghel 8Devender Baghel 8
Here is code that worked for me  cheers :)

public class BusScheduleCache {
    //add variable
    private Cache.OrgPartition part;
    
    //get partition
     String partition = 'local.BusSchedule';
    
    //custom Constructor
    public BusScheduleCache(){
        Cache.OrgPartition orgPart = new Cache.OrgPartition(partition);
        if(orgPart != null){
            part = orgPart;
        }
    }
    
    public void putSchedule(String busLine, Time[] schedule){ 
       //store values in cache
        if(part!=null){
            part.put('busLine','schedule');
        }
    }
    
    public Time[] getSchedule(String busLine){
        //return schedule for bus time 
        Time schedule = (Time)part.get(busLine);
        Time[] schedules = new List<Time>();
        if(schedule != null){
            schedules.add(schedule);
        }else{
            Time t1 = Time.newInstance(8, 0, 0, 0);
            Time t2 = Time.newInstance(17, 0, 0, 0);
            schedules.add(t1);
            schedules.add(t2);   
        }   
          
        System.debug('Schetude here : '+schedules);
        return schedules;
    }   
    
}
CHoughCHough
Hi everyone,

I have been able to get my code to work and tested it with my own test class successfully but I still cannot get a pass on this Trailhead module. I did some more digging in the developer console debugger by looking a the log entry that is created when I press "Check Challenge..". If I display the "Source" window of the log in the Developer Console, I believe that I can see the code that is being run to test my solution (using the "All" perspective).  It looks like the trailhead code is populating a schedule with name of '411' and a value of 10am and 12pm (See line 2 in the 1st screenshot below). It then tests that the first array element is 8am (See line 6), which fails. My log shows "Assertion Failed: Expected: 08:00:00.000Z, Actual: 10:00:00:000Z" which makes sense given the code I see in this Source view.

Here is the Source of what I believe is the Trailhead test:
Source
Here is my log entry error:
Log

Can anyone confirm if this test is still working for them and if so, do you have different source displayed? Am I misunderstanding the debug results that I am seeing here?

Thank you for your help.

Charley
CHoughCHough
Just posting this followup on my above comments. The Trailhead test was to be done with a zero cache allocation. I tested my code both without and with a 5mb allocation but ran the trailhead test with the allocation still in place. Once I removed the allocation, the test passed.

Charley
AVINASH NAIR 7AVINASH NAIR 7
THANKS  @ Lehel Gyeresi
Gaurav SadhwaniGaurav Sadhwani

This one worked for me :

public class BusScheduleCache {

    private Cache.OrgPartition part;
    
    public BusScheduleCache(){
        part = new Cache.OrgPartition('local.BusSchedule');
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        part.put(busLine, schedule);
    }
    
    public Time[] getSchedule(String busLine) {
        Time[] timearray = new List<Time>();
        Time[] cachedTime = (Time[])part.get(busLine);
        if(cachedTime != null) {
            return cachedTime;
        } else {
            Time t1 = Time.newInstance(8,0,0,0);		//hrs,min,sec,milsec
            Time t2 = Time.newInstance(17,0,0,0);
            timearray.add(t1);
            timearray.add(t2);
            return timearray;
        }
    }   
}
Luis Paulo 2Luis Paulo 2
Hi,

First:
From Setup ->  Platform Cache -> Create Platform Cache Partition with the name as BusSchedule.

Apex Class
public class BusScheduleCache{
    private Cache.OrgPartition part;        
    public BusScheduleCache(){
        part = new Cache.OrgPartition('local.BusSchedule');
    }
    
    public void putSchedule(String busLine, Time[] schedule){
        if(part != null){
            part.put('busLine', schedule);
        }
    }
    
    public Time[] getSchedule(String busLine){
        Time[] schedule = (Time[])part.get(busLine);
        if (schedule != null) {
            return schedule;  
        } else {
            return new List<Time>{Time.newInstance(8, 0, 0, 0), Time.newInstance(17, 0, 0, 0)};
        }
    }
}