You need to sign in to do that
Don't have an account?
Maxence LEFEBVRE
Apply Service Layer Principles in Apex Trailhead - Issue submitting challenge
Hello everyone,
The challenge is the following :
My Service class is the following:
And my webservice endpoint (I can't really call it restful) is :
I tried in an anonymous code execution :
Then, using workbeng, I tried the following request :
And got a 200 response from the server as well as
That's why I am still confused on why the challenge checker is rejecting me ?
Did I miss something the challenge requested me to do ?
The challenge is the following :
Create a Service and Implement a Caller.
Create an Apex class that is a service that exposes a bulkified service method. The service closes and sets the reason for one or more given case records. Implement an Apex REST class that calls this service.
Create an Apex class that is a service that exposes a bulkified service method. The service closes and sets the reason for one or more given case records. Implement an Apex REST class that calls this service.
- Create an Apex class called CaseService that contains a void static method called closeCases that takes a set of Case IDs and a String parameter for the close reason.
- Create a REST Apex class called CaseCloseResource with a URL mapping /case/*/close (where * will be the Id). Implement a POST closeCase method which accepts a String reason and calls the CaseService.closeCases service method passing in the Id and close reason.
Challenge Not yet complete... here's what's wrong:
The Apex service does not appear to be closing cases correctly with the specified reason.
The Apex service does not appear to be closing cases correctly with the specified reason.
My Service class is the following:
global with sharing class CaseService { global static void closeCases(List<Id> caseIds, String closingReason) { if (caseIds == null || caseIds.size() == 0) { throw new CaseServiceException('You should tell me which cases you wanna close'); } if (closingReason == null || closingReason == '') { throw new CaseServiceException('I won\'t close a case without any reason'); } List<Case> closedCases = new List<Case>(); for(Id caseId : caseIds) { Case caseToClose = new Case( Id = caseId, Status = 'Closed', Reason = closingReason ); closedCases.add(caseToClose); } SavePoint sp = Database.setSavepoint(); if (closedCases.size() > 0) { try { update closedCases; } catch (Exception e) { Database.rollback(sp); throw e; } } } public class CaseServiceException extends Exception {} }
And my webservice endpoint (I can't really call it restful) is :
@RestResource(urlMapping='/case/*/close') global with sharing class CaseCloseResource { @HttpPost global static void closeCase(String reason) { Id id = RestContext.request.requestURI.substringBetween('case/', '/close'); CaseService.closeCases(new Id[]{id}, reason); } }
I tried in an anonymous code execution :
Id id = '5000Y000002Vc8Y'; CaseService.closeCases(new Id[]{id}, 'You shall be closed!');And successfully got the following :
Changed Case Reason from Installation to You shall be closed!.
Changed Status from New to Closed.
Changed Status from New to Closed.
Then, using workbeng, I tried the following request :
POST /services/apexrest/case/5000Y000002Vc8m/close Payload : {"reason" : "Please close this case too!"}
And got a 200 response from the server as well as
Changed Case Reason from Feedback to Please close this case too!.
Changed Status from New to Closed.
Changed Status from New to Closed.
That's why I am still confused on why the challenge checker is rejecting me ?
Did I miss something the challenge requested me to do ?
Thank you very much for your help.
As it happens, I mistook Set<Id> for List<Id> meaning the challenge couldn't find a method with a correct signature.
I think the Trailhead challenge validator should be more transparent in its error message :)
Final code :
All Answers
Here is my code:
public with sharing class CaseService {
public static void closeCases(set<ID> caseIDs, String newReason)
{
//Validate parameters
if(caseIDs ==null || caseIDs.size() == 0)
{
throw new CaseServiceException('Cases not specified');
}
if(newReason == null || newReason == '')
{
throw new caseServiceException('Invalide reason provided');
}
//Query cases
List<Case> casesToClose = [select Id, Status, Reason from Case where Id IN :caseIDs];
List<Case> updatedCases = new List<Case>();
for(Case caseToUpd : casesToClose)
{
caseToUpd.Status = 'Closed';
caseToUpd.Reason = newReason;
updatedCases.add(caseToUpd);
}
//Update database
SavePoint sp = Database.setSavepoint();
try
{
update updatedCases;
}
catch(Exception e)
{
//Rollback
Database.rollback(sp);
//Throw expecetion on to caller
throw e;
}
}
public class CaseServiceException extends Exception{}
}
Thank you very much for your answer.
I tried this way too, with the following service class :
Unfortunately, the challenge still can't be validated, the same error is still returning
The Apex service does not appear to be closing cases correctly with the specified reason.
I unit tested with the following class and the test suite is all green !
@RestResource(URLMapping = '/case/*/close')
global with sharing class CaseCloseResource {
@HttpPost
global static void closeCase(String closureReason)
{
//Parse request
RestRequest req = RestContext.request;
String[] idsList = req.requestURI.split('/');
ID caseIDs = idsList[2];
//Call the service
CaseService.closeCases(new Set<ID> {caseIDs}, closureReason);
}
}
Thank you very much for your help.
As it happens, I mistook Set<Id> for List<Id> meaning the challenge couldn't find a method with a correct signature.
I think the Trailhead challenge validator should be more transparent in its error message :)
Final code :