You need to sign in to do that
Don't have an account?
Lehel 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.
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?
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.
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?
All Answers
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:
If there's no confirmed bug in the challenge, I'd like to receive a hint (not the whole passing code).
Thanks in advance.
right? We already create a Cache partition through salesforce Classic earlier on.
This solution also works, Confirming for 2018
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;
}
}
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:
Here is my log entry error:
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
Charley
This one worked for me :
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)};
}
}
}
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[] cachedRate = (Time[])part.get(busLine);
if(cachedRate == null){
Time[] schedule = new Time[]{Time.newInstance(8,0,0,0), Time.newInstance(17,0,0,0)};
System.debug('time ' + schedule);
return schedule;
}
return cachedRate;
}
}