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 →
You can download file directly from github by Click Here.
Method:- GET (Opportunity Record)
@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)
@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)
//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)
@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)
@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)
@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]
@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;
}
Further post that would you like to learn in Salesforce
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
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 |