• Charles Stevenson
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello everyone,

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 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.
I keep getting the following error when trying to submit my solution on "Apply Service Layer Principles in Apex"
Challenge Not yet complete... here's what's wrong: 
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.

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.

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 ?