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
dai tran 12dai tran 12 

How can define multi function get or post in a class of webservice?

This my webservice:
@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {
global static Map<String,List<Case>> getCaseById() {
        RestRequest request = RestContext.request;       
        String caseId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        List<Case> result;   
          result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
                        FROM Case Where Id=:caseId];     
        Map<String,List<Case>> res=new Map<String,List<Case>>();
        res.put('Cases', result);
        return res;
    }
 @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;
    }   
 @HttpPost
  global static Map<String,List<Case>> getDataGroupOfUser(String userid, String group)
   {     
       List<Case> result;   
          result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
                        FROM Case Where userId__c=:userid and group__c=:group];     
        Map<String,List<Case>> res=new Map<String,List<Case>>();
        res.put('Cases', result);
        return res;
    }   
}

Error: Only one method per type can be defined with: HttpPost
How can define multi function get or post in a class of webservice?
Shruti SShruti S
The problem here is that - you have two POST methods. This is not allowed in the same class. All the HTTP Verbs in the Apex Class exposing the REST Services should have different or unique verbs such as 1 GET, 1 POST and so on.
Raj VakatiRaj Vakati
You cannt able to do it in salesforce .. in only class you can able to define only one HTTP actions per type 


If you want to do this  .. workaroung will 

Define a one POST method whihc will accept the flag .. based on the flag return the result ..

 
@HttpPost
    global static Object common(String subject, String status,
        String origin, String priority , boolen isCreate ) {
        if(isCreate){
			// create logic 
		}else{
		//other logic 	
		}
    }   
 
}


 
Shruti SShruti S
+1 Raj Vakati

The idea here is to use the same @HttpPost method to perform two actions by distinguishing your inputs.

In here we are sending a parameter called "action" which defines if we need to do a Retrieve/Fetch or a Creation.

In the example below, I am using the same POST method to retrieve details of "John Doe" in one case while in the other we are trying to create a new Contact with the name - "Mary".
@HttpPost
public RestResponse process( String action, String data ) {
    ....

    if( action == 'receive' ) {
        //do something
    }
    else if( action == 'send' ) {
        //do something
    }

    ....
}
POST
/apexrest/contacts/process?action=receive&data=john.doe@example.com
--Gives you back details of John Doe

POST
/apexrest/contacts/process?action=send&data=Mary
--Creates a new Contact named Mary
dai tran 12dai tran 12
Thank you ,Raj vakati,
But it occur error: HttpPost methods do not support return type of Object