How to run Apex REST API Post/Put/Patch/Get/Delete through Workbench into Opportunity object in Salesforce | Steps to call apex rest service Post/Put/Patch/Get/Delete from workbench rest explorer in Salesforce | How to make call to apex rest api in Workbench in Salesforce

3,362 views


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 β†’

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

call apex rest service from workbench rest explorer -- w3web.net

 

You can download file directly from github by Click Here.

 

Method:- GET (Opportunity Record)

  1.     @httpGet
  2.     global static Opportunity getOpportunityById(){
  3.          RestRequest request = RestContext.request;
  4.          RestResponse response  = RestContext.response;
  5.         //grab the oppId FROM the END OF the URL       
  6.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  7.         Opportunity RESULT = [SELECT Id, Name, LeadSource, Closedate, Probability FROM Opportunity WHERE Id =:oppId];
  8.         RETURN RESULT;
  9.     }

 
call apex rest service from workbench rest explorer -- w3web.net
 

Method:- Post (Opportunity Record)

  1.    @httpPost
  2.     global static ID createOpportunity(String name, String stage, string closeDate, string SOURCE){
  3.         Opportunity newOpp = NEW Opportunity(
  4.              name=name,
  5.              stageName=stage,
  6.              leadSource=SOURCE,
  7.              closeDate=DATE.valueOf(closeDate)
  8.         );
  9.         INSERT newOpp;
  10.         RETURN newOpp.Id;
  11.     }

 

call apex rest service from workbench rest explorer -- w3web.net
 

Method:- Post with (Convert PDF to Base64, Send Attachment to Salesforce)

  1.     //CONVERT PDF TO Base64:-https://base64.guru/converter/encode/pdf
  2.     @httpPost
  3.     global static ID createOpportunity(String name, String stage, string closeDate, string SOURCE, string attachfilestr, string blobStr){
  4.          Opportunity newOpp = NEW Opportunity();
  5.          newOpp.Name=name;
  6.          newOpp.stageName=stage;
  7.          newOpp.leadSource=SOURCE;
  8.          newOpp.closeDate=DATE.valueOf(closeDate);
  9.          INSERT newOpp;
  10.  
  11.         ContentVersion fileContentObj = NEW ContentVersion();
  12.          fileContentObj.ContentLocation = 'S'; 
  13.          fileContentObj.PathOnClient = attachfilestr;
  14.          fileContentObj.Title = attachfilestr;
  15.          fileContentObj.VersionData = EncodingUtil.base64Decode(blobStr);  
  16.          fileContentObj.FirstPublishLocationId  = newOpp.Id;
  17.          INSERT fileContentObj;
  18.  
  19.         RETURN newOpp.Id;
  20.     }

workbench salesforce rest api -- w3web.net

  

Method:- Put (Opportunity Record)

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    @httpPut
  2.     global static ID createOpportunity(String name, String stage, string closeDate, string SOURCE, string id){
  3.         Opportunity newOpp = NEW Opportunity(
  4.              name=name,
  5.              stageName=stage,
  6.              leadSource=SOURCE,
  7.              closeDate=DATE.valueOf(closeDate),
  8.              Id=id
  9.         );
  10.         upsert newOpp;
  11.         RETURN newOpp.Id;
  12.     }

 
call apex rest service from workbench rest explorer -- w3web.net
 

Method:- Patch (Opportunity Record)

  1.     @HttpPatch
  2.     global static ID updateOppFlds(){
  3.         RestRequest request = RestContext.request;
  4.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  5.          Map<String, Object> mapObj = NEW Map<String, Object>(); 
  6.          Opportunity thisOpportunity = [SELECT Id FROM Opportunity WHERE Id =:oppId];
  7.           Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
  8.  
  9.         FOR(String fieldName:params.keySet()){
  10.            thisOpportunity.put(fieldName,params.get(fieldName));
  11.         }
  12.  
  13.         UPDATE thisOpportunity;
  14.         RETURN thisOpportunity.Id;
  15.     }

 
call apex rest service from workbench rest explorer -- w3web.net
 

Method:- Delete (Opportunity Record)

  1.   @httpDelete    
  2.     global static void deleteOppsRec(){
  3.         RestRequest request = RestContext.request;
  4.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  5.          Opportunity thisOppRec = [SELECT Id FROM Opportunity WHERE Id =:oppId];
  6.         DELETE thisOppRec;
  7.     }

 
call apex rest service from workbench rest explorer -- w3web.net
 

Find complete code

Create Apex Class : apexRestAPI.apxc

From Developer Console >> File >> New >> Apex Class

apexRestAPI.apxc [Apex Class Controller]

  1.    @RestResource(urlMapping = '/opportunityApexRest/*')
  2. global WITH sharing class apexRestAPI {
  3.  
  4.     @httpGet
  5.     global static Opportunity getOpportunityById(){
  6.          RestRequest request = RestContext.request;
  7.          RestResponse response  = RestContext.response;
  8.         //grab the oppId FROM the END OF the URL       
  9.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  10.         Opportunity RESULT = [SELECT Id, Name, LeadSource, Closedate, Probability FROM Opportunity WHERE Id =:oppId];
  11.         RETURN RESULT;
  12.     }
  13.  
  14.  
  15.    //CONVERT PDF TO Base64:-https://base64.guru/converter/encode/pdf
  16.     @httpPost
  17.     global static ID createOpportunity(String name, String stage, string closeDate, string SOURCE, string attachfilestr, string blobStr){
  18.          Opportunity newOpp = NEW Opportunity();
  19.          newOpp.Name=name;
  20.          newOpp.stageName=stage;
  21.          newOpp.leadSource=SOURCE;
  22.          newOpp.closeDate=DATE.valueOf(closeDate);
  23.          INSERT newOpp;
  24.  
  25.         ContentVersion fileContentObj = NEW ContentVersion();
  26.          fileContentObj.ContentLocation = 'S'; 
  27.          fileContentObj.PathOnClient = attachfilestr;
  28.          fileContentObj.Title = attachfilestr;
  29.          fileContentObj.VersionData = EncodingUtil.base64Decode(blobStr);  
  30.          fileContentObj.FirstPublishLocationId  = newOpp.Id;
  31.          INSERT fileContentObj;
  32.  
  33.         RETURN newOpp.Id;
  34.     }
  35.  
  36.  
  37.     @httpPut
  38.     global static ID createOpportunity(String name, String stage, string closeDate, string SOURCE, string id){
  39.         Opportunity newOpp = NEW Opportunity(
  40.              name=name,
  41.              stageName=stage,
  42.              leadSource=SOURCE,
  43.              closeDate=DATE.valueOf(closeDate),
  44.              Id=id
  45.         );
  46.         upsert newOpp;
  47.         RETURN newOpp.Id;
  48.     }
  49.  
  50.  
  51.  
  52.     @HttpPatch
  53.     global static ID updateOppFlds(){
  54.         RestRequest request = RestContext.request;
  55.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  56.          Map<String, Object> mapObj = NEW Map<String, Object>(); 
  57.          Opportunity thisOpportunity = [SELECT Id FROM Opportunity WHERE Id =:oppId];
  58.           Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
  59.  
  60.         FOR(String fieldName:params.keySet()){
  61.            thisOpportunity.put(fieldName,params.get(fieldName));
  62.         }
  63.  
  64.         UPDATE thisOpportunity;
  65.         RETURN thisOpportunity.Id;
  66.     }
  67.  
  68.  
  69.    @httpDelete    
  70.     global static void deleteOppsRec(){
  71.         RestRequest request = RestContext.request;
  72.          String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  73.          Opportunity thisOppRec = [SELECT Id FROM Opportunity WHERE Id =:oppId];
  74.         DELETE thisOppRec;
  75.  
  76.  
  77.  
  78. }

 

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses β†’

πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment