3

Salesforce provides a example of how to define a custom rest endpoint and how to make calls to that endpoint. It is not working for me. Why? I have successfully retrieved an OAuth token that I have successfully used with the REST Api.

Here is the Rest class in apex:

@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {

@HttpDelete
global static void doDelete() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    Account account = [SELECT Id FROM Account ];
    delete account;
}

@HttpGet
global static Account doGet() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    Account result = [SELECT Id, Name, Phone, Website FROM Account];
    return result;
}

@HttpPost
global static String doPost(String name,
    String phone, String website) {
    Account account = new Account();
    account.Name = name;
    account.phone = phone;
    account.website = website;
    insert account;
    return account.Id;
}
}

and here is some python code that I am using to make the get request:

import requests

url = 'https://na15.salesforce.com/services/apexrest/Account/001i000000VdaoqAAB'
headers = {'Authorization': 'Bearer 00Di0000000iQ.....'}
r = requests.get(url, headers=headers) 
print r.text

I am getting the following error:

[{"message":"Could not find a match for URL /Account/001i000000VdaoqAAB","errorCode":"NOT_FOUND"}]

Is there some setting that I need to enable?

2 Answers 2

8

Not sure on that error but I do see a couple of potential issues that might be causing you problems

The get is setup wrongly - when I tried that, I got an error because it is not filtering by the Id you pass in - see line highlighted below from docs that you are missing. As such, it needs to returns a list of records (the code does work when you change the return type)

@HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        **String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);**
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
        return result;
    }

Also, I probably wouldn't name the RestResource 'Account' as that is a standard resource name...wondering if that is causing a conflict?

Update: If you have a namespace enabled for your org, then it will need to be included in the url. In your case it would be:

https://na15.salesforce.com/services/apexrest/your_namespace/Account/001i000000VdaoqAAB where "your_namespace" is the namespace of your org.

7
  • I see what you are saying, but I am getting a 404 and not a 500. I would assume that the error you found in my code would cause a 500 error. I've also tried changing the url mapping to /helloworld and that doesn't work either. When you ran my code did you get a 500 or 404? Commented Feb 13, 2014 at 20:15
  • I actually got an Apex error returned because the get returned more than one record - so the call itself worked fine. Commented Feb 13, 2014 at 20:16
  • Also the 'Account' url mapping was provided in the official salesforce documentation, so I don't think that is the problem. Commented Feb 13, 2014 at 20:16
  • 3
    just to confirm - does this org have a namespace enabled? If so, you need to add that to the URL... Commented Feb 13, 2014 at 20:45
  • 2
    https://<instance name> .salesforce.com/services/apexrest/<namespace>/hello Commented Feb 13, 2014 at 21:05
0

See if this blog post helps out: http://blog.jeffdouglas.com/2012/02/28/apex-rest-service-not-found/

2
  • That might be it. I just refactored one of the core classes of my app, and I haven't fixed the unit tests yet. They don't run or compile at the moment. I will let you know if that fixed it once I fix the compilation errors and run the tests. Commented Feb 13, 2014 at 16:30
  • I've fixed all of my unit tests and ran them all, but I am still getting the 404 not found error. Do I have to create a unit test for the controller/endpoint as well? Commented Feb 13, 2014 at 20:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .