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
Yogesh K MishraYogesh K Mishra 

How to create a single rest API or URL to create, update and delete a record in salesforce?

How to create a single rest API or URL to create, update and delete a record in salesforce? The API can be call from other system Like logic app or ServiceNow.?

I want to create a single API to perform all the 3 operation in apex http call.
Requirement is we only need single URL, anyone can help, Much appriciated...
ANUTEJANUTEJ (Salesforce Developers) 
Hi Yogesh,

I think you can create a single custom rest resource and you can have annotation that can execute the method as per the request, below is an example:
 
@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;
    }    
}
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Carlos Carvalho JrCarlos Carvalho Jr
Hi Yogesh, the correct way is to create all methods separately, just like that:
 
@RestResource(urlMapping='/v1/Customer/*')
global without sharing class AccountServiceRest  {

    @HttpGet
    global static void doGet() {
		//YOUR CODE
    }

    @HttpPost
    global static void doPost( ) 
    {
		//YOUR CODE
    }


    @HttpPut
    global static void doPut() 
    {
		//YOUR CODE
    }


    @HttpDelete
    global static void doDelete() {
		//YOUR CODE
    }
}

But if you really need just one method you can use parameters:
 
@RestResource(urlMapping='/v1/Customer/*')
global without sharing class AccountServiceRest  {


    @HttpPost
    global static void doPost( ) 
    {
	RestRequest request = RestContext.request; 
    	RestResponse response = RestContext.response; 
	
       	String myfunction= request.params.get('function');

        if( myfunction.equalsIgnoreCase('A') ){
            //do something
        }else if(myfunction.equalsIgnoreCase('B')){
           //do something
        }
    }
}


tks,