5

This is a newb question.

The following is my Apex REST Class.

@RestResource(urlMapping='/Expense/*')
global class MyExpenseWebService
{

@HttpGet
    global static Expense__c getExpense()
    {
        RestRequest req = RestContext.request;
        String eId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

        Expense__c e = [SELECT Id,Name,Amount__c,Comments__c,Expense_Date__c FROM Expense__c WHERE Id = :eId];
        return e;
    }

@HttpPost
global static String createExpense(String Type,Integer Amount,String Comments)
{
    Expense__c e =  new Expense__c(Type__c = Type, Amount__c = Amount, Comments__c = Comments);
    insert e;
    return e.Id;

}    



}

I tried to add another HttpGet method but I got a compiler error.

From the compiler error I discerned that only one occurrence of a specific type of HTTP method is allowed per class.

Can someone tell me why ?

1
  • 1
    How would you know which Get was going to be executed? Commented Feb 2, 2015 at 19:08

1 Answer 1

8

The HTTP verb is GET and the URL of the request is matched against the urlMapping associated with the class. There is no more information available to Salesforce to know which class/method to invoke - hence the limitation on a single GET, POST, PATCH, PUT, DELETE method in the class.

Use either multiple REST classes with different URL patterns or use information within the parm string of the GET to dispatch logic in the GET method.

Think hard about ensuring your URL is really RESTFul; that way of thinking helps structure your class(es).

You must log in to answer this question.

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