Hey guys, today in this post we are going to learn about How to run Apex REST API Post/Put/Patch/Get/Delete through Workbench into Opportunity object in Salesforce.
A REST resource is an abstraction of a piece of information or an action, such as a single data record, a collection of records, or a query. Each resource in REST API is identified by a named Uniform Resource Identifier (URI) and is accessed using standard HTTP methods (HEAD, GET, POST, PATCH, DELETE). REST API is based on the usage of resources, their URIs, and the links between them. To know more details about Apex Rest API, Click Here.
Final Output →
Method:- GET (Opportunity Record)
1 2 3 4 5 6 7 8 9 |
@httpGet global static Opportunity getOpportunityById(){ RestRequest request = RestContext.request; RestResponse response = RestContext.response; //grab the oppId from the end of the URL String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Opportunity result = [Select Id, Name, LeadSource, Closedate, Probability From Opportunity Where Id =:oppId]; return result; } |

Method:- Post (Opportunity Record)
1 2 3 4 5 6 7 8 9 10 11 |
@httpPost global static ID createOpportunity(String name, String stage, string closeDate, string source){ Opportunity newOpp = new Opportunity( name=name, stageName=stage, leadSource=source, closeDate=date.valueOf(closeDate) ); insert newOpp; return newOpp.Id; } |
Method:- Post with (Convert PDF to Base64, Send Attachment to Salesforce)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//Convert PDF to Base64:-https://base64.guru/converter/encode/pdf @httpPost global static ID createOpportunity(String name, String stage, string closeDate, string source, string attachfilestr, string blobStr){ Opportunity newOpp = new Opportunity(); newOpp.Name=name; newOpp.stageName=stage; newOpp.leadSource=source; newOpp.closeDate=date.valueOf(closeDate); insert newOpp; ContentVersion fileContentObj = new ContentVersion(); fileContentObj.ContentLocation = 'S'; fileContentObj.PathOnClient = attachfilestr; fileContentObj.Title = attachfilestr; fileContentObj.VersionData = EncodingUtil.base64Decode(blobStr); fileContentObj.FirstPublishLocationId = newOpp.Id; insert fileContentObj; return newOpp.Id; } |
Method:- Put (Opportunity Record)
1 2 3 4 5 6 7 8 9 10 11 12 |
@httpPut global static ID createOpportunity(String name, String stage, string closeDate, string source, string id){ Opportunity newOpp = new Opportunity( name=name, stageName=stage, leadSource=source, closeDate=date.valueOf(closeDate), Id=id ); upsert newOpp; return newOpp.Id; } |

Method:- Patch (Opportunity Record)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@HttpPatch global static ID updateOppFlds(){ RestRequest request = RestContext.request; String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Map<String, Object> mapObj = new Map<String, Object>(); Opportunity thisOpportunity = [Select Id From Opportunity Where Id =:oppId]; Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring()); for(String fieldName:params.keySet()){ thisOpportunity.put(fieldName,params.get(fieldName)); } update thisOpportunity; return thisOpportunity.Id; } |

Method:- Delete (Opportunity Record)
1 2 3 4 5 6 7 |
@httpDelete global static void deleteOppsRec(){ RestRequest request = RestContext.request; String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Opportunity thisOppRec = [Select Id From Opportunity Where Id =:oppId]; delete thisOppRec; } |

Find complete code
Create Apex Class : apexRestAPI.apxc
From Developer Console >> File >> New >> Apex Class
apexRestAPI.apxc [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
@RestResource(urlMapping = '/opportunityApexRest/*') global with sharing class apexRestAPI { @httpGet global static Opportunity getOpportunityById(){ RestRequest request = RestContext.request; RestResponse response = RestContext.response; //grab the oppId from the end of the URL String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Opportunity result = [Select Id, Name, LeadSource, Closedate, Probability From Opportunity Where Id =:oppId]; return result; } //Convert PDF to Base64:-https://base64.guru/converter/encode/pdf @httpPost global static ID createOpportunity(String name, String stage, string closeDate, string source, string attachfilestr, string blobStr){ Opportunity newOpp = new Opportunity(); newOpp.Name=name; newOpp.stageName=stage; newOpp.leadSource=source; newOpp.closeDate=date.valueOf(closeDate); insert newOpp; ContentVersion fileContentObj = new ContentVersion(); fileContentObj.ContentLocation = 'S'; fileContentObj.PathOnClient = attachfilestr; fileContentObj.Title = attachfilestr; fileContentObj.VersionData = EncodingUtil.base64Decode(blobStr); fileContentObj.FirstPublishLocationId = newOpp.Id; insert fileContentObj; return newOpp.Id; } @httpPut global static ID createOpportunity(String name, String stage, string closeDate, string source, string id){ Opportunity newOpp = new Opportunity( name=name, stageName=stage, leadSource=source, closeDate=date.valueOf(closeDate), Id=id ); upsert newOpp; return newOpp.Id; } @HttpPatch global static ID updateOppFlds(){ RestRequest request = RestContext.request; String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Map<String, Object> mapObj = new Map<String, Object>(); Opportunity thisOpportunity = [Select Id From Opportunity Where Id =:oppId]; Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring()); for(String fieldName:params.keySet()){ thisOpportunity.put(fieldName,params.get(fieldName)); } update thisOpportunity; return thisOpportunity.Id; } @httpDelete global static void deleteOppsRec(){ RestRequest request = RestContext.request; String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); Opportunity thisOppRec = [Select Id From Opportunity Where Id =:oppId]; delete thisOppRec; } |
How do I call a custom REST API from workbench?
Choose the GET method in the workbench. Enter the custom API in the URL. Add the Case id as a parameter and hit the “Execute” button. This will execute the Get method within the Salesforce apex class.
What is the difference between REST API and Apex REST API?
The REST API (Force.com REST API) is the generic API provided by Salesforce. On the other hand, the Apex REST API is an API written by yourself in apex to provide custom methods.
How do I run apex in workbench?
Navigate to Utilities à Apex Execute. Enter the Apex Code in the provided space, that work as an Anonymous window. Select the 'Log category' as 'Apex Code' and 'Log Level' as 'Debug'. Click the 'Execute' button to see the results of the Apex Execution in the same window.
Related Topics | You May Also Like
- Your reaction of the article ▾