Introduction to ApexRest

How to Develop ApexRest Services

In this post, we will learn how we can develop the ApexRest Services. We will cover the below points: –

  1. What is ApexRest?
  2. When/Why developer should develop apex rest?
  3. Apex REST Annotations
  4. Explanation of Methods
  5. Sample Apex Class
  6. Explanation of URL
  7. Test Apex Rest using Workbench
  8. Things to keep in mind

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.

  1. @RestResource(urlMapping=’/yourUrl/’) Used at the top of the class which tells the URL to be hit from the 3rd party.
  2. @HttpDelete: – used to delete a particular record from Salesforce
  3. @HttpGet: – To get/ retrieve the record from Salesforce
  4. @HttpPatch: – Used to update existing record in salesforce if the record exists otherwise to create a new record.
  5. @HttpPost: – used to Create a record in Salesforce
  6. @HttpPut: – To update the existing record in Salesforce

Sample Apex Class: – 

@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(){}
}

 

Explanation of URL: – 

https://instance.salesforce.com/services/apexrest/packageNamespace/Your URL/

  1. https://instance.salesforce.com/: – Your Salesforce ORG instance URL
  2. services/apexrest/ : – URL which tell that the URL is exposed via APEX REST(Sub URL)
  3. packageNamespace/ :- Your org namespace if any(optional)
  4. Your URL:- URL that you specified in your Class

Things to keep in mind: – 

  1. Every @RestResource class can have only one method with the same annotation. For Example, Class can have only one method with @HttpGet, @httpDelete, @HttpPost and etc methods.
  2. IF two class have the same urlMapping then class with the latest modified date will be considered.
  3. If you are calling @httpPatch from the 3rd party then You need to append ?_httpMethod=PATCH at the end of the URL and set the POST as the method.
  4. RestRequest and RestResponse objects are available by default in your Apex methods through the static RestContext object.

 

Below is the class that I created for the practice purpose

@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; }
    }
}

 

See the video for the complete tutorial: – 

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

 

2 thoughts on “Introduction to ApexRest

Leave a Reply

Your email address will not be published. Required fields are marked *