-3

I have trigger on custom object and i need to make callout (Post) from trigger whenever status field change to completed I know i can do this using @future in trigger but my requirement is if response is not success then again i needs make callout this process should go for three attempt then need to send email to group that three unsuccessful attempt completed. Is there any way/possible to do this ?

        HttpResponse res = http.send(req);
        if(res.getStatusCode() == 200)
            system.debug('Success');

        else{
        system.debug('Not success');
        //I am thinking to add callout here then again check for success and fail and do same          

        } 
  • 4
    What have you tried? Where are you stuck? – David Reed Jul 16 at 1:49
  • @DavidReed : Added some code from my trigger handler but not sure if this will work or not all this code get executed inside future – Sfdc_1184 Jul 16 at 2:15
  • 1
    I didn't understand 3 times because if a status code is a failure maybe there is an issue in your request. I mean if it is a failure with specific status code even you tried 100 times still it is a failure. – Pavan tej Jul 16 at 13:59
3

You can use queueable apex. If the callout fails then chain the same job by using system.enqueueJob method.

Below is the way how you can do a callout from queuable apex, you have to implement Database.AllowCallouts interface.

public class myClass implements Queueable, Database.AllowsCallouts
{
  public void execute(QueueableContext context) {
      //your callout code here
   if(callOutIsNotSuccess){
     System.enqueueJob(this); // this will queue your job again! - Be aware of the governor limts for queueable apex. You can use limits class to check the same
    }

  }
}
  • I used Queueable but never used for callout can you provide some more detail how this scenario will be achieved by queueable – Sfdc_1184 Jul 16 at 2:19
  • @Sfdc_1184 I have provided the details in my above comment. – San Jul 16 at 13:48
  • 1
    ``System.enqueueJob(this);` is bad architecture, it can span infinitely spanning queuable in case the 3rd party service goes down – Pranay Jaiswal Jul 16 at 14:25
  • You can add a counter to the Queueable that increments with each attempt, and quits after some reasonable number (e.g. 3-5) – Charles T Jul 16 at 22:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.