Salesforce Trigger with Http Callout



Salesforce Trigger with Http Callout

This blog will provide the information to implementation HTTP Callout from Trigger.
Salesforce has two ways to execute a callout from a trigger is to run it asynchronously. There are following :

  • From @future method : Use when only single callout in Trigger
  • From Queueable Interface Class : Use when need multiple callout in Trigger
Let’s do implement and enjoy this magic!!

From @future method

Future methods execute asynchronously.Future Apex is used to run processes in a separate thread, at a later time when system resources become available.

Some important point for future method

  • Method should be return void type.
  • Method should be static.
  • Method support only primitive data types.
  • Method support callout with allow callout.(Default is callout = false).


trigger AccountTrigger on Account (after insert,after update) {
    if(Trigger.isAfter){
        for(Account acc: Trigger.New){
            if(!System.isFuture() && !System.isBatch()){
                UtilWebservice.callWebService();
            }
        }
    }
}




public class UtilWebservice{
    @future(callout=true)
    public static void callWebService() {
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = '<End Point>';
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        HttpResponse response = new HTTP().send(request);
        if (response.getStatusCode() == 200) {
            //Any DML Opreation
        } 
    }
}


From Queueable Interface Class

Queueable Apex Salesforce is more advanced and enhanced version of future methods with some extra features.

Some important point for future method

  • Class should be implements Queueable interface
  • Execute Method should be used in Class.
  • Parameter can be Non-primitive types like sObject.
  • You can use future method in Helper Class for any other operation.


trigger AccountTrigger on Account (after insert,after update) {
    if(Trigger.isAfter){
        for(Account acc: Trigger.New){
            if(!System.isFuture() && !System.isBatch()){
                System.enqueueJob(new UtilWebserviceQueueable(Trigger.New));
            }
        }
    }
}




public class UtilWebserviceQueueable implements Queueable,Database.AllowsCallouts{
    List<Account> accountList;
    public GoogleDriveIntegrationQueueable(List<Account> accountList){
        this.accountList= accountList;        
    }
    public void execute(QueueableContext qc){
        for(Account acc : accountList){              
            UtilWebserviceQueueable.callOneWebService();
        }        
    }
}




public class UtilWebserviceQueueable{
    public static void callOneWebService() {
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = '<End Point>';
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        HttpResponse response = new HTTP().send(request);
        if (response.getStatusCode() == 200) {
            //Call another callout
            callTwoWebService();
        } 
    }
    public static void callTwoWebService() {
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = '<End Point>';
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        HttpResponse response = new HTTP().send(request); 
    }
}



Happy Sharing...

Everyone has their own favorites, so please feel free to share yours in the comments below!

Comments

Popular posts from this blog

Salesforce LWC : Compact Layout on Hover

Salesforce LWC With Third Party JS : D3

User Data Privacy