Last active
March 14, 2019 00:09
-
-
Save SalesforceBobLightning/88b8c8843b84e5f91ce1cc78624d4bba to your computer and use it in GitHub Desktop.
Salesforce Chained Queueable 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 without sharing class ChainedQueueable implements Queueable { | |
private Set<Id> ids; | |
public ChainedQueueable(Set<Id> ids) { | |
this.ids = ids; | |
} | |
public void execute(QueueableContext context) { | |
if (ids.size() == 0) { | |
return; | |
} | |
// get next id | |
Id recordId = getNextId(); | |
// do something with the record | |
// create new job and process next record | |
processNextRecord(); | |
} | |
private void processNextRecord() | |
{ | |
enqueueJob(this.ids); | |
} | |
public static void enqueueJob(Set<Id> ids) { | |
if (ids.size() == 0) { | |
return; | |
} | |
// create next job | |
ChainedQueueable job = new ChainedQueueable(ids); | |
// queue next job | |
Id jobId = System.enqueueJob(job); | |
System.debug(jobId); | |
} | |
private Id getNextId() { | |
// get next record Id | |
Id recordId; | |
for(Id item : ids) { | |
recordId = item; | |
break; | |
} | |
// remove recordId | |
ids.remove(recordId); | |
return recordId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment