Update Attempt to Chain in Helper Class -- Not Working -- Separate Class Still Works.
My last update was premature. This doWork method still does not control the order of the calls once they are queued. Still working with this solution.
public with sharing class SandboxRefreshHelper {
public static void doWork(){
SandboxDataRefresh.getFullBoxParentCampaigns();
System.enqueueJob(new async1());
}
public class async1 implements Queueable{
public void execute(QueueableContext con){
SandboxDataRefresh.getFullBoxChildCampaigns();
System.enqueueJob(new async2());
}
}
public class async2 implements Queueable{
public void execute(QueueableContext con){
System.debug('WE ARE DONE');
}
}
}
When I attempt to chain in this way. The future methods are not being called in order. Seems odd, since the documentation explicitly states that this is a purpose of chaining is to chain callouts. So I'm not sure what I'm missing here. However if I put the callout in it's own class entirely, then the chain works as expected.
public with sharing class SandboxRefreshHelper implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext con){
System.debug('START CHAIN');
SandboxDataRefresh.getFullBoxParentCampaigns();
System.enqueueJob(new async1());
}
public class async1 implements Queueable{
public void execute(QueueableContext con){
SandboxDataRefresh.getFullBoxChildCampaigns();
System.enqueueJob(new async2());
}
}
public class async2 implements Queueable{
public void execute(QueueableContext con){
System.debug('WE ARE DONE');
}
}
}
Suppose that I have the following Class
public with sharing class QueueableExample implements Queueable{
public void execute(QueuableContext con){
method1(); //future method
method2(); //future method
method3(); //future method
}
}
Question
Is it possible to chain these methods and control their execution without creating multiple classes for each method? In my specific use case, I am doing some basic GET methods that insert records into my salesforce org from another salesforce org.
The only solution I have been able to do is if I have another class setup this way:
My Solution
Create a new class for each method that I want queued up in the chain.
public with sharing class QueuableExample2 implements Queueable{
public void execute(){
mySecondMethod()//future
}
@future(callout=true)
public mySecondMethod(){
//logic to do callout and insert records
}
}
public with sharing class QueueableExample implements Queueable{
public void execute(QueuableContext con){
method1(); //future method fires first
System.enqueueJob(new QueuableExample2()); //future method fires second
System.enqueueJob(new QueuableExample3()); //future method fires third
System.enqueueJob(new QueuableExampleChainPlusOne()); //future method fires n+1 where n = class.
}
}
Queueable
andCallable
- you'd have chain of jobs to do, each calling a method on aCallable
. Would be fund to implement.