+ Start a Discussion
Linked2MarkLinked2Mark 

APEX integration services Trailhead

I am working through APEX Web services in the Apex Integration Services Trailhead. I would like to get the APEX super badge so that I can take the Platform Dev 2 Cert Test. I am afraid my debugging skills arent so super at the moment. :-(

I have the following code (directly from the Trailhead) 

@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {

    @HttpGet
    global static Case getCaseById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String caseId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
                        FROM Case
                        WHERE Id = :caseId];
        return result;
    }

    @HttpPost
    global static ID createCase(String subject, String status,
        String origin, String priority) {
        Case thisCase = new Case(
            Subject=subject,
            Status=status,
            Origin=origin,
            Priority=priority);
        insert thisCase;
        return thisCase.Id;
    }   

    @HttpDelete
    global static void deleteCase() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        delete thisCase;
    }     

    @HttpPut
    global static ID upsertCase(String subject, String status,
        String origin, String priority, String id) {
        Case thisCase = new Case(
                Id=id,
                Subject=subject,
                Status=status,
                Origin=origin,
                Priority=priority);
        // Match case by Id, if present.
        // Otherwise, create new case.
        upsert thisCase;
        // Return the case ID.
        return thisCase.Id;
    }

    @HttpPatch
    global static ID updateCaseFields() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        // Deserialize the JSON string into name-value pairs
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
        // Iterate through each parameter field and value
        for(String fieldName : params.keySet()) {
            // Set the field and value on the Case sObject
            thisCase.put(fieldName, params.get(fieldName));
        }
        update thisCase;
        return thisCase.Id;
    }    

}
When I use Workbench to hit the url: /services/apexrest/Cases/

I get an error:

HTTP/1.1 404 Not Found Date: Tue, 27 Feb 2018 23:10:19 GMT Strict-Transport-Security: max-age=31536002; includeSubDomains Public-Key-Pins-Report-Only: pin-sha256="9n0izTnSRF+W4W4JTq51avSXkWhQB8duS2bxVLfzXsY="; pin-sha256="5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w="; pin-sha256="njN4rRG+22dNXAi+yb8e3UMypgzPUPHlv4+foULwl1g="; max-age=86400; includeSubDomains; report-uri="https://calm-dawn-26291.herokuapp.com/hpkp-report/00D37000000JkeWp"; X-Robots-Tag: none Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private Set-Cookie: BrowserId=hI2Gh0BrQFCMm-_6mBuA7Q;Path=/;Domain=.salesforce.com;Expires=Sat, 28-Apr-2018 23:10:19 GMT;Max-Age=5184000 Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked
[ { "errorCode" : "NOT_FOUND", "message" : "Could not find a match for URL" } ]

 

Seems like this ought to work. Any ideas what I am doing wrong?

 

Many thanks

Raj VakatiRaj Vakati
Please use resource like this 
/services/apexrest/Cases/*

 
Raj VakatiRaj Vakati
i mean REST API resource URL shold be  from workbench 
/services/apexrest/Cases/

 
Linked2MarkLinked2Mark
Hi Raj. Tried that  - it is exactly what I used the first time time. I thought maybe there was a small difference but the uri you suggest produces exactly the same error.
 
Raj VakatiRaj Vakati
Can you share the screenshot of workbench? Please check you logged into the workbench with correct user ?  

Have you selected the correct HTTP methods? and Passing the body?  

 
Linked2MarkLinked2Mark
Here ya go....
User-added image
Linked2MarkLinked2Mark
I checked and can see that I am logged into the correct Org in workbench. Those are often the kinds of mistakes that would cause this.
Raj VakatiRaj Vakati
Everything looks good for me 

But can you try one more time with this URI. Keep Star also  and share the screenshot pls 

 
/services/apexrest/Cases/*

 
Linked2MarkLinked2Mark

No I figured it out. The answer is a bit buried but it had to do with the namespace of my dev org. The hint is here:

https://salesforce.stackexchange.com/questions/69289/could-not-find-a-match-for-url-workbench

I ran this piece of APEX to get the exact namespace:

string np = [SELECT NamespacePrefix FROM Organization].NamespacePrefix;
The namespace is Linked2V1

I then ran the uri from workbench as follows:
/services/apexrest/Linked2V1/Cases/

Many thanks for your help!

Raj VakatiRaj Vakati
Yes .. that is the issue... 

Close this thread ..  :)