You need to sign in to do that
Don't have an account?
Apply Service Layer Principles in Apex Trailhead - Issue Submitting the challenge
All, I am a bit stuck to complete the challenge of the Apply Service Layer Principles in Apex trail.
I have created what has been requested and tested it successfully but I keep getting the following error:
Challenge Not yet complete... here's what's wrong:
The Apex service does not appear to be closing cases correctly with the specified reason.
Any idea on how to debug and findout what is wrong?
Below you can see my code:
Class CaseService:
public class CaseService
{
public static void closeCases(Set<ID> cases, String closeReason)
{
System.debug('SKA - ' + cases);
System.debug('SKA - ' + closeReason);
List<Case> caseToUpdate = new List<Case>();
for (Id caseId : cases)
{
Case c = new Case(Id = caseId);
c.Reason = closeReason;
c.Status = 'Closed';
caseToUpdate.add(c);
}
update caseToUpdate;
}
}
Class CaseCloseResource:
@RestResource(urlMapping='/case/*/close')
global class CaseCloseResource
{
@httpPost
global static void closeCase(String closeReason)
{
System.debug('SKA - HTTPPOST - ' + closeReason + ' - ' + RestContext.request.requestURI);
// Parse context
RestRequest req = RestContext.request;
String[] uriParts = req.requestURI.split('/');
Id caseId = uriParts[2];
// Call the service
CaseService.closeCases(new Set<ID> { caseId }, closeReason);
}
}
I have created what has been requested and tested it successfully but I keep getting the following error:
Challenge Not yet complete... here's what's wrong:
The Apex service does not appear to be closing cases correctly with the specified reason.
Any idea on how to debug and findout what is wrong?
Below you can see my code:
Class CaseService:
public class CaseService
{
public static void closeCases(Set<ID> cases, String closeReason)
{
System.debug('SKA - ' + cases);
System.debug('SKA - ' + closeReason);
List<Case> caseToUpdate = new List<Case>();
for (Id caseId : cases)
{
Case c = new Case(Id = caseId);
c.Reason = closeReason;
c.Status = 'Closed';
caseToUpdate.add(c);
}
update caseToUpdate;
}
}
Class CaseCloseResource:
@RestResource(urlMapping='/case/*/close')
global class CaseCloseResource
{
@httpPost
global static void closeCase(String closeReason)
{
System.debug('SKA - HTTPPOST - ' + closeReason + ' - ' + RestContext.request.requestURI);
// Parse context
RestRequest req = RestContext.request;
String[] uriParts = req.requestURI.split('/');
Id caseId = uriParts[2];
// Call the service
CaseService.closeCases(new Set<ID> { caseId }, closeReason);
}
}
This solved this issue. No clue why it was not working in my initial org.
All Answers
This solved this issue. No clue why it was not working in my initial org.
public with sharing class CaseService {
public static void closeCases(Set<Id> caseIds, String reason) {
List<Case> casesToUpdate = [SELECT Status, Reason FROM Case WHERE Id in :caseIds];
for (Case c : casesToUpdate) {
c.Status = 'Closed';
c.Reason = reason;
}
update casesToUpdate ;
}
}
@Harbir S Chahal, thank you for sharing your code. My SOQL query was using...<code>Id =: caseId<code> instead of... <code>Id IN :caseId<code>
Also, I tried creating a separate List to update instead of using the same list recursively if anyone else tries a similar solution.
I ran into a similar issue as posted originally. I had to change my Service class and add 'with sharing' and then it worked.