Ways To invoke REST Callouts From Apex

We can invoke REST callout using Apex. There are 6 approaches to achieve. They are as follows
1.Using Scheduled Class
2.Using Batch Class
3.Using AJAX Tool Kit
4.Using Visualforce Page
5.Using Queueable
6.Using Triggers

Let us learn each one of them.

Approach: 1
Callouts using Schedule class:
            Yes, we can do callout from schedule class but it should be inside the asynchronous method.
Callouts can be using @future annotation.
Example:
If you observe the "WarehouseCalloutService" class,"runWarehouseEquipmentSync()" method is annotated with @future. If we remove this annotation then it will throw below error.


Below is the Schedule class :

Below is the class which contains Callout 

Approach: 2
Callouts using Batch Class:
1. Callout logic can be implemented using Batch class by specifying in any of the methods(Start or execute or finish). For this, the batch class needs to implement  "Database.AllowsCallouts" interface. For schedule class, again you do not need to implement "Database.AllowsCallouts" interface.

2. Once the callout logic is written in the batch class, you can simply call from the schedule class.

Below is the batch class with callout logic inside start() method 

Below is the schedule class 

Below is the batch class with callout logic inside execute() method
Developers Forums

Approach: 3
AJAX Toolkit
                 Using "AJAX Toolkit" also we can perform callouts.
If your requirement is to make a callout on button click with out any implementation of the class,then AJAX Toolkit is your solution.

Example:  I want to create a button on the Account record detail page, now whenever I click a button then the callout response needs to store in "Description" filed.


   This is interesting, right ?? you can find more examples in this AJAX Toolkit Developer Guide


Approach: 4
Using Visualforce Page
            Using Visualforce Page, We can make callouts in 2 ways, They are
1.Synchronous Callouts.
2.Asynchronous Callouts.

For more make information about Synchronous and Asynchronous future callouts, Please refer the here.

Callouts using continuation can be referred here. 
Approach: 5
Using Queueable 
You have to implement the "Database.AllowsCallouts" interface along with the "Queueable" interface to enable callouts, just like in batch apex and scheduled apex.
        
  From spring17 onwards, Apex allows web service callouts from chained queueable jobs. Previously, a queueable job could make a web service callout, but additional chained jobs that made callouts would throw exceptions. For more information click here.


Approach: 6
Using Triggers

Nature of Triggers = synchronous execution = Each line in Triggers or it's helper class, will move to next line only if the previous line executed successfully.

So, inside trigger if you write callout logic it will wait for callout response, as a result, we might hit governor limits. To avoid this type of situations, Salesforce design is preventing us to directly implement the callout logic inside the trigger.

Alternatively, you can place the callout code in an asynchronous method that’s annotated with @future(callout=true) or use Queueable Apex. This way, the callout runs on a separate thread, and the execution of the calling method isn’t blocked.



pseudocode :



Please do comment if you know any other approaches..!!


tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments

  1. Could you please send me the Salesforce developer material to raghu442sfdc@gmail.com thanks in advance

    ReplyDelete
  2. Thanks Manjunath ...u have tought us in very much understandable manner ..I really appreciate it and keep waiting for ur next article 🙏

    ReplyDelete

Post a Comment