Best Practices: Callout retry

I’m trying to implement an auto-retry method on callout fail.

I notice that is better to wait some prudential time to make the retry call, to avoid flooding issues on remote server.

By now, I’m thinking on implement a schedulable interface to retry using something like this:

try {
    //Execute web service call here     
    HTTPResponse res = http.send(req);  

} catch(System.CalloutException e) {
    //Exception handling goes here.... retry the call
    //instance an schedulable class
}  

Questions:

Is it possible to instance a scheaduble class and schedule that job to N minutes after exception was cath?

What could be the best approach to give retryClass the number of attemps already executed? using SchedulableContext ?

Is there any other workarround ?

Answer

I would suggest that you can insert the failed record Id into a separate custom object and also stamp the time your batch started .Then when your batch finishes you can schedule the job on the custom object to pick the inserted records due to failed transaction and remember to mark a check box or delete from the custom object if the transaction succeeded .

Caution:There are limits on no of scheduled jobs at a time .

Attribution
Source : Link , Question Author : Martin Borthiry , Answer Author : Mohith Shrivastava

Leave a Comment