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
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 = '';
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 accountList;
public GoogleDriveIntegrationQueueable(List 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 = '';
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 = '';
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);
}
}
Comments
Post a Comment