function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephane KarolinskiStephane Karolinski 

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);  
        
    }
}
 
Best Answer chosen by Stephane Karolinski
Stephane KarolinskiStephane Karolinski
All, I did connect to a different org and copy/paste my code.
This solved this issue. No clue why it was not working in my initial org.

All Answers

Stephane KarolinskiStephane Karolinski
All, I did connect to a different org and copy/paste my code.
This solved this issue. No clue why it was not working in my initial org.
This was selected as the best answer
Subbu KaruppiahSubbu Karuppiah
I am running into the same issue. I tested through REST explorer and my service closes the case with the reason passed in. I also tried connecting to three different orgs and the same issue.
Harbir S ChahalHarbir S Chahal
All, I submitted my challenge with below CaseService class code and it worked fine on same Org. The code for CaseCloseResource class is same as @Stephane.

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 ;
    }
}

 
Dhanik L SahniDhanik L Sahni
Worked for me.
Swapnagandha HawaldarSwapnagandha Hawaldar
I ran into the same issue. Tried everything but couldn't get rid of the error. Then just on a whim, I changed the List of cases to a Set of cases. And it worked! I have no idea why changing from a List to a Set should work but there it is.
Donald DedmanDonald Dedman
@Swapnagandha Hawaldar...most likely because Trailheads are very specific with solutions.  Note a reminder that Sets don't allow duplicates whereas Lists do.

@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.
ChellappaChellappa
Hi All,

I ran into a similar issue as posted originally. I had to change my Service class and add 'with sharing' and then it worked.