Set<Id> recordIds = new Set<Id>();
new ExampleChainedQueueable(recordIds).enqueueJob();
Last active
March 14, 2019 14:46
-
-
Save SalesforceBobLightning/03e1fb77e317d4ddeb4b393794daf65e to your computer and use it in GitHub Desktop.
Salesforce Chained Queueable Abstract Apex Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AbstractChainedQueueable implements Queueable { | |
private Set<Id> recordIds; | |
public AbstractChainedQueueable(Set<Id> recordIds) { | |
this.recordIds = recordIds; | |
} | |
public abstract void processRecord(Id recordId); | |
public abstract AbstractChainedQueueable createJob(Set<Id> ids); | |
public void execute(QueueableContext context) { | |
if (recordIds.size() == 0) { | |
return; | |
} | |
Id recordId = getNextInvoiceId(); | |
processRecord(recordId); | |
processNextInvoice(); | |
} | |
private void processNextInvoice() | |
{ | |
enqueueJob(this.recordIds); | |
} | |
private void enqueueJob(Set<Id> ids) { | |
if (ids.size() == 0) { | |
return; | |
} | |
// create next job | |
AbstractChainedQueueable job = createJob(ids); | |
// queue next job | |
Id jobId = System.enqueueJob(job); | |
System.debug(jobId); | |
} | |
public void enqueueJob() { | |
// queue job | |
Id jobId = System.enqueueJob(this); | |
System.debug(jobId); | |
} | |
private Id getNextInvoiceId() { | |
// get next record | |
Id recordId; | |
for(Id item : recordIds) { | |
recordId = item; | |
break; | |
} | |
// remove recordId | |
recordIds.remove(recordId); | |
return recordId; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleChainedQueueable extends AbstractChainedQueueable { | |
public ExampleChainedQueueable(Set<Id> recordIds) { | |
super(recordIds); | |
} | |
public override void processRecord(Id recordId) { | |
// do work here | |
} | |
public override AbstractChainedQueueable createJob(Set<Id> ids) { | |
return new ExampleQueueable(ids); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment