Scheduling a MergeUp Batch to Start LaterLearn how to start MergeUp Batches at any date and time in the future, and on repeating schedules.Jan 20, 2025Knowledge
InformationArticle BodyPrerequisites Create a MergeUp Batch record or records, whether manually, by a Flow, or by Apex (To learn how to create a MergeUp Batch by any of these methods, see Generating Multiple Documents Based on a Report, Generating Multiple Documents Using Flows, or Generating Multiple Documents Using Apex).Have a basic understanding of the Apex Schedulable Interface. Overview Once a MergeUp Batch is created, it must be run in order to merge documents and upload them to records. These steps will walk you through starting one or more MergeUp Batches at some time in the future, as well as how to set up a recurring schedule of batch merges. Steps Schedule a MergeUp Batch to Start Later To schedule a MergeUp Batch or Batches for some future date/time, we need to use an Apex class that implements the schedulable interface, and takes in a list of MergeUp Batches to merge on execute. global class Schedule_MergeUp_Batch implements Schedulable { private List<mergeup__MergeUp_Batch__c> batches; public Schedule_MergeUp_Batch(List<mergeup__MergeUp_Batch__c> batches) { this.batches = batches; } } Next we add the execute method, and in it we start all the MergeUp Batches that have been passed in in the constructor. For more information on starting MergeUp Batches in this way, see Starting Multliple MergeUp Batches. global class Schedule_MergeUp_Batch implements Schedulable { private List<mergeup__MergeUp_Batch__c> batches; public Schedule_MergeUp_Batch(List<mergeup__MergeUp_Batch__c> batches) { this.batches = batches; } global void execute(SchedulableContext SC) { if(!this.batches.isEmpty()) { for (mergeup__MergeUp_Batch__c batch : this.batches) { batch.mergeup__Status__c = 'Started'; } update this.batches; ) } } Finally, to schedule these batches to start when we want them to, we schedule the class in anonymous Apex, using the MergeUp Batches that we want to schedule when we construct the class. In this example we'll use a specific group of MergeUp Batches. Since these MergeUp Batches were selected specifically, the cron expression should be constructed to run at a single specific date/time. For example, here is a schedule that will start the batches on December 24th, 2024 at 12:00pm. Schedule_MergeUp_Batch job = new Schedule_MergeUp_Batch([ SELECT Id FROM mergeup__MergeUp_Batch__c WHERE Name IN ('MU-BATCH-000004', 'MU-BATCH-000005', 'MU-BATCH-000006') ]); String cronExpr = '0 0 12 24 12 ? 2024'; String jobID = System.schedule('Start MergeUp Batches Job', cronExpr, job); To learn more about how to write your own cron expression, including examples, see the System.schedule Method in the Apex Reference Guide. Schedule Recurring MergeUp Batches For this example we will create an Apex schedulable class that starts all unstarted MergeUp Batches every day at 11:00pm. To start with we create a new Apex class that implements the schedulable interface. This time we query all the records in the execute function. global class Start_MergeUp_Batches_Eod implements Schedulable { global void execute(SchedulableContext SC) { List<mergeup__MergeUp_Batch__c> batches = [ SELECT Id FROM mergeup__MergeUp_Batches__c WHERE mergeup__Status__c = 'Not Started' ]; if (!batches.isEmpty()) { for (mergeup__MergeUp_Batch__c batch : ) { batch.mergeup__Status__c = 'Started'; } update batches; } } } To schedule the class we use anonymous Apex, and a cron expression that will run the class every day at 11:00pm. Start_MergeUp_Batches_Eod job = new Start_MergeUp_Batches_Eod(); String cronExpr = '0 0 23 * * ?'; String jobID = System.schedule('Daily EOD MergeUp Batches Job', cronExpr, job); Notes Since the Apex Scheduler is a part of Asyncronous Apex, the time that you schedule your batches to run may not be the exact time that they start. For more information on this and other limitations, see Apex Scheduler | Apex Developer Guide | Salesforce Developers. Next Steps Learn how to monitor your MergeUp Batch(es): Monitor a MergeUp BatchMonitor Multiple MergeUp BatchesMonitor the MergeUp Batch Apex Job TitleScheduling a MergeUp Batch to Start LaterURL NameScheduling-a-MergeUp-Batch-to-Start-Later