Created
October 6, 2019 01:13
-
-
Save melissajhansen/0db014e506641ef5d54c5c1ca19d6b9c to your computer and use it in GitHub Desktop.
A queueable job that will update any number of SObjects, chaining another job with any objects that could not be updated within limits
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
//Used to queue the updating of a list of SObjects | |
//Usage: | |
//ID jobID = System.enqueueJob(new QueueableObjectUpdater(<SOBJECTSTOUPDATE>)); | |
public class QueueableObjectUpdater implements Queueable { | |
public List < SObject > objectsToUpdate; | |
public List < SObject > objectsToQueueForUpdate; | |
//single object constructor | |
public QueueableObjectUpdater( SObject objectToUpdate) { | |
this.objectsToUpdate = new List < SObject >(); | |
this.objectsToUpdate.add(objectToUpdate); | |
} | |
//list constructor | |
public QueueableObjectUpdater(List < SObject > objects) { | |
Integer dmlRowLimit = Limits.getLimitDmlRows(); | |
if (this.objectsToUpdate.size()<=dmlRowLimit) { | |
//We are under the limit, no need to queue up another job, everything goes in the update list | |
this.objectsToUpdate = objects; | |
} else { | |
// We are trying to update more records than we have in our limits. Do up to the limit, then queue the remainder for the next job | |
// each job will whittle the list down until we're done! | |
// we will use a helper method to divide our big list into the first batch and future batch | |
this.objectsToUpdate = HelperFunctions.subsetSobjects(objects, 0, dmlRowLimit, false); | |
this.objectsToQueueForUpdate = HelperFunctions.subsetSobjects(objects, dmlRowLimit, objects.size()-dmlRowLimit, false); | |
} | |
} | |
public void execute (QueueableContext context) { | |
system.debug('objectsBeingUpdatedCount'+ objectsToUpdate.size()); | |
update objectsToUpdate; | |
// If we have objects to queue up for updating in the next batch, do so now | |
if (this.objectsToQueueForUpdate!=null && this.objectsToQueueForUpdate.size()>0) { | |
ID jobID = System.enqueueJob(new QueueableObjectUpdater(this.objectsToQueueForUpdate)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment