When/Why developer should develop apex rest? - A Developer should develop the ApexRest when there is a need to send the data to an external third party or you wanted to get the data from the third party.
In this post, we will learn how we can develop the ApexRest Services. We will cover the below points: –
What is ApexRest? – ApexRest is nothing, it is a way by which we can expose the Salesforce Object Record.
When/Why developer should develop apex rest? – A Developer should develop the ApexRest when there is a need to send the data to an external third party or you wanted to get the data from the third party.
Apex REST Annotations –
There are 6 annotations available that enable you to expose an Apex class as a RESTful Web service.
Sample Apex Class: –
[codesyntax lang=”java” container=”div” doclinks=”1″]
@RestResource(urlMapping=‘/v1/studentManagement/*’)
global with sharing class StudentManager {
@HttpGet
global static void doGetStudent() {}
@HttpPost
global static void doCreateStudent() {}
@HttpPut
global static void doUpdateStudent(){}
@HttpDelete
global static void doDeleteStudent(){}
}
[/codesyntax]
Explanation of URL: –
https://instance.salesforce.com/services/apexrest/packageNamespace/Your URL/
Things to keep in mind: –
Below is the class that I created for the practice purpose
[codesyntax lang=”java” container=”div” doclinks=”1″]
@RestResource(urlMapping='/v1/BookManagement/')
global class BookManager {
@httpGet
global static Book__c doGetBook(){
Book__c book = new Book__c();
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
book = [Select Id, Name, Price__c From Book__c Where Id =: bookId];
return book;
}
@httpDelete
global static String doDeleteBook(){
Book__c book = new Book__c();
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
book = [Select Id, Name, Price__c From Book__c Where Id =: bookId];
delete book;
return 'Record Deleted!';
}
@httpPost
global static Book__c doCreateBook(BooksInfo bookInfo){
Book__c book = bookInfo.book;
insert book;
return book;
}
@httpPut
global static Book__c doUpdateBook(String Name){
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
Book__c book = new Book__c(Name = name, Id = bookId);
update book;
return book;
}
global class BooksInfo{
public Book__c book { get; set; }
}
}
[/codesyntax]
See the video for the complete tutorial: –
[embedyt] https://www.youtube.com/embed?listType=playlist&list=PLV3ll8m0ZlprOM7rkyw6-NMfcehp8vHIx&v=mnYAArjQRBg&layout=gallery[/embedyt]
Thanks for reading. Sharing is Caring 🙂
If you have any doubts then please come up in the comments section with OR You can contact me using my twitter handle – cloudyamit
Good topic for apex rest.
Very helpful for me
Thanks Amit
Thank you Rajesh and Glad you liked it.
Please share as sharing is caring and have planned some more interesting topics related to Integration with Salesforce.
Hi Amit,
Nice Explanation on Apex Rest.
I need some clarifications.
1) When we will use HTTP Callout and Apex Rest.
2) if you have any example on how to get the contact from 3rd party and insert into contact in Salesforce.
Thanks.
Hi Shri,
It completely depends upon what kind of API it is but
1 – If we wanted to integrate with Web Application or Mobile Application Application then we generally prefer REST
2 – For platform to platform we prefer SOAP (Salesforce – Salesforce OR Salesforce to any API)
For inserting the data you can refer my videos on sfdcpanther youtube channel and you will get all the doubts clear there.
Thanks,
SFDCPanther
Hi Amit, your posts are always very helpful.
My query is
In what circumstances we use
1. REST API
2. SOAP API
MULESOFT
LIGHTNING CONNECT
DELL BHOOMI