Hey guys, today in this post we are going to learn about How can we expose Salesforce data as a REST API? | Salesforce Rest API Controller @HttpGet, @HttpPost, @HttpPut, @HttpPatch or @HttpDelete | How We can expose Salesforce data as a REST API using Apex REST Services in Salesforce.
We can expose Salesforce data as a REST API using Apex REST Services. To do this, we create an Apex class and use the @RestResource annotation to make it accessible from external systems.
Then, we define methods like @HttpGet, @HttpPost, @HttpPut, or @HttpDelete inside that class to perform CRUD operations.
π§© Key Points to Remember:
1οΈβ£ Enable API Access
- Ensure that the Profile or Permission Set of the integration user has API Enabled.
- Also, the connected app should be configured if youβre using OAuth for authentication.
2οΈβ£ Create an Apex REST Resource Class
|
|
@RestResource(urlMapping='/CreateContactAPI/*')
global class salesforceRestApiCtrl {//USING @HttpPost (Correct Way TO INSERT Contact)
//@HttpPost
global static String createContact() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
try{// Parse incoming JSON DATA
String requestBody = req.requestBody.toString();
Map<String, Object> DATA = (Map<String, Object>) JSON.deserializeUntyped(requestBody);
// CREATE NEW Contact record
Contact con = NEW Contact();
con.FirstName = (String)DATA.get('FirstName');
con.LastName = (String)DATA.get('LastName');
con.Email = (String)DATA.get('Email');
con.Phone = (String)DATA.get('Phone');
INSERT con;//res.statusCode = 201;
RETURN 'β Contact Created Successfully with Id: ' + con.Id;
}catch(Exception e){
system.debug('Exception ' + e );
RETURN 'β Error: ' + e.getMessage();
}}//Workbench//URL: /services/apexrest/CreateContactAPI/
//Body:/*{"FirstName": "Vijay","LastName": "Kumar","Email": "vijay.kumar@example.com","Phone": "9876543210"}*///USING @HttpPost (Correct Way TO GET Contact)
//@HttpGet
global static String getContact() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
system.debug('req ' + req);
system.debug('res ' + res);
try{String firstName = req.params.get('firstName');
String lastName = req.params.get('lastName');
String email = req.params.get('email');
String phone = req.params.get('phone');
Contact con = NEW Contact();
con.FirstName = firstName;
con.LastName = lastName;
con.Email = email;
con.Phone = phone;
INSERT con;RETURN 'β Contact Created Successfully via GET with Id: ' + con.Id;
}catch(Exception e){
//res.statusCode = 400;
RETURN 'β Error: ' + e.getMessage();
}}//How TO Test (IN Browser OR Workbench):
////GET /services/apexrest/CreateContactGetAPI/?firstName=Vijay&lastName=Kumar&email=vijay.kumar@example.com&phone=9876543210
//// ======================================
// PUT β FULL UPDATE Contact (REPLACE ALL)
// ======================================
@HttpPut
global static String updateContact() {
RestRequest req = RestContext.request;
String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
Map<String, Object> reqBody = (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.toString());
Contact con = [SELECT Id, FirstName, LastName, Email, Phone FROM Contact WHERE Id = :contactId LIMIT 1];
con.FirstName = (String)reqBody.get('FirstName');
con.LastName = (String)reqBody.get('LastName');
con.Email = (String)reqBody.get('Email');
con.Phone = (String)reqBody.get('Phone');
UPDATE con;RETURN 'Contact updated successfully using PUT. Id: ' + con.Id;
}//Workbench///services/apexrest/CreateContactAPI/003J100000AD3yTIAT
/*{"FirstName": "Amit","LastName": "Verma","Email": "amit@example.com","Phone": "8888888888"}*/// πΉ Partial Contact UPDATE (USE: PATCH)
@HttpPatch
global static String patchContact() {
RestRequest req = RestContext.request;
String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
Map<String, Object> reqBody = (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.toString());
Contact con = [SELECT Id, FirstName, LastName, Email, Phone FROM Contact WHERE Id = :contactId LIMIT 1];
IF(reqBody.containsKey('Phone')) con.Phone = (String)reqBody.get('Phone');
IF(reqBody.containsKey('Email')) con.Email = (String)reqBody.get('Email');
UPDATE con;RETURN 'Contact partially updated using PATCH. Id: ' + con.Id;
}//Workbench///services/apexrest/CreateContactAPI/003J100000AD3yTIAT
/*{"FirstName": "Amit","LastName": "Verma","Email": "amit@example.com","Phone": "8888888888"}*/// πΉ DELETE Contact Record (USE: DELETE)
@HttpDelete
global static String deleteContact() {
RestRequest req = RestContext.request;
String contactId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
Contact con = [SELECT Id FROM Contact WHERE Id = :contactId LIMIT 1];
DELETE con;RETURN 'Contact deleted successfully. Id: ' + contactId;
}//Workbench///services/apexrest/CreateContactAPI/003J100000AD3yTIAT
}
Further post that would you like to learn in Salesforce
Does Salesforce have a REST API?
REST API provides you with programmatic access to your data in Salesforce. The flexibility and scalability of REST API make it an excellent choice for integrating Salesforce into your applications and for performing complex operations on a large scale.
How to enable Salesforce REST API?
Go to Manage Users and click Profiles. Click Edit on the specific profile you're updating. Scroll down to Administrative Permissions and check the API Enabled box. Click Save.
Is Salesforce API free?
Free Salesforce APIs are also available. The primary purpose of these APIs is to help develop new applications and process existing data. Most of these APIs will provide access to basic Salesforce features, such as data, objects, and programmatic access.
Related Topics | You May Also Like
|
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β |

