+ Start a Discussion
SFDC INSFDC IN 

How to retry the callout on http request timed out in rest apex callouts?

Below is the code. Can anyone tell how to implement retry logic when the 2 minutes http request get timed out.
 
public static void sendOptyInfo(Set <Id> oppIdSet) {
    try{
        for (Opportunity opp: [SELECT id ,StageName, Account.Name  from Opportunity where id in: oppIdSet]){
            
            String jsonBody = JSON.serialize(new Wrapper(opp));
            HttpRequest request = new HttpRequest();
            HttpResponse response = new HttpResponse();
            
            Http http = new Http();
            
            Api_Request__c apiReq = new Api_Request__c();
            apiReq.Opportunity__c = opp.id;
            apiReq.Stage_Name__c = opp.StageName;
            apiReq.Account_Name__c = opp.Account.Name;
            
            insert apiReq;
            
            request.setEndpoint('');
            request.setHeader('Content-Type','application/json'); 
            request.setMethod('GET');
            request.setBody(jsonBody);
            request.setTimeout(120000);
            system.debug('opp json' +jsonBody);
            
            response = http.send(request);
            if (response.getStatusCode() == 200) {
                System.debug('Response-' + response);
                Object results = (Object) JSON.deserializeUntyped(response.getBody());
                
            }
        }
        
    }
    catch(System.CalloutException e){
        System.debug('Error-' + e.getMessage());   
    }
}

 
AnudeepAnudeep (Salesforce Developers) 
In general, the retry is attempted after the exception is triggered. If I have to explain with a skeleton, it looks like this
 
try {
    //Execute web service call here     
    HTTPResponse res = http.send(req);  

} catch(System.CalloutException e) {
    //Exception handling goes here.... retry the call
    //retry logic goes here
}

Here is a sample code
 
public class SelfServe implements Schedulable {

    public String Token = null;

    public void execute(SchedulableContext ctx) {        

        RetrieveMachineDetails Job = new RetrieveMachineDetails();
            Job.Token = Token;    
        System.enqueueJob(Job);

    }     

    public class RetrieveMachineDetails implements Queueable, Database.AllowsCallouts {

        public String Token = null;

        public void execute(QueueableContext context){                              

            Http http = new Http();
            HttpRequest request = new HttpRequest();        
                    request.setEndpoint('https://app.endpoint.com/api');
                    request.setMethod('POST');
                    request.setHeader('Content-Type', 'application/json;charset=UTF-8');                 
                    request.setBody('{"query":  "$message_=\'METRIC\' && $tier=\'customer\' "}');                                                                                                                                                                       
            HttpResponse response = http.send(request);              

            if (response.getStatusCode() == 200) {
                //do stuff     
            }

            else { //retry

                DateTime Dt = system.now().addMinutes(5);            
                   String day = string.valueOf(Dt.day());                    
                   String month = string.valueOf(Dt.month());                      
                   String hour = string.valueOf(Dt.hour());                   
                   String minute = string.valueOf(Dt.minute());                                   
                   String year = string.valueOf(Dt.year());                  
                String ScheduledTime = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;

                SelfServe Job = new SelfServe();
                    Job.Token = Token;
                System.schedule('SelfServe Job'+Dt, ScheduledTime, Job);

            }
        }   
    } 
}
NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you