1

I'm having trouble with my current callout/trigger setup.

My current architecture is like this:

Scheduled job - runs every 5 minutes:

global class Scheduled implements Schedulable {
    global void execute(SchedulableContext ctx) {
        CalloutClass.CalloutMethod();
    }
}

CalloutClass:

public class CalloutClass{
    @future(callout=true)
    public static void CalloutMethod() {
        //Make Callout
        //Get response
        //Update object
    } 
}

Trigger Class - This is called from the actual trigger and is triggered by CalloutClass.CalloutMethod():

public class ObjectTriggerHelper {
    public static void TriggerHelper() {
        AnotherCalloutClass.CalloutMethod();
    }
}

AnotherCalloutClass:

public class AnotherCalloutClass {
    @future(callout=true)
    public static void CalloutMethod() {
        //Make another callout
        //Update object
        //chain ends here
    }
}

My problem is that the scheduled job requires my CalloutClass.CalloutMethod() to be future since it's coming from a scheduled job. But this gives me an error because AnotherCalloutClass.CalloutMethod() is also future, and you can't invoke a future method from a future method. But I can't make AnotherCalloutClass.CalloutMethod() not future, since it's also being called from a trigger.

What would be the best way to take care of this? Put all the callout code in AnotherCalloutClass into the object trigger? Or will I have to do something with Queueable jobs?

Thank you!

1 Answer 1

0

Yes, you cannot call future method from another future method.

But you can call a future method from a queuable method and a queuable from another queuable.

I would advise making both CalloutClass and AnotherCalloutClass as queuable

public class CalloutClass implements Queueable, Database.AllowsCallouts{    

    public static void execute() {
        //Make Callout
        //Get response
        //Update object
    } 
}


public class AnotherCalloutClass implements Queueable, Database.AllowsCallouts{    

    public static void execute() {
        //Make another callout
        //Update object
        //chain ends here
    }
}


public class ObjectTriggerHelper {
    public static void TriggerHelper() {
        System.enqueueJob(new AnotherCalloutClass());
    }
}

global class Scheduled implements Schedulable {
    global void execute(SchedulableContext ctx) {
       System.enqueueJob(new CalloutClass());
    }
}
3
  • This looks like it would help! Thank you! How would I pass arguments from ObjectTriggerHelper.TriggerHelper() into the AnotherCalloutClass()?
    – Douglas
    Jun 25, 2020 at 20:49
  • 1
    Create a parameterised constructor in queueable class accept values as class level variables and then use them in execute? Jun 25, 2020 at 20:50
  • Ah I see. I was curious if the QueueableContext object could be used to be make it a bit cleaner, but a constructor would work fine too. Thank you!
    – Douglas
    Jun 25, 2020 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .