Creating a MergeUp Batch from a List of Records in ApexLearn how to use Apex to create a MergeUp Batch from a list of records.Jan 21, 2025Knowledge
InformationArticle BodyPrerequisites At least one MergeUp Template created for each Object type you wish to merge in a batch.An understanding of Salesforce's propreitary server-side language Apex. Overview Salesforce's Apex server-side language provides the ultimate flexiblility and power inside of Salesforce. This walkthrough shows how to use Apex to create a MergeUp Batch from a list of sObjects. Steps In order to create our batch, we need to get the Id of the MergeUp Template we want to use, and a list of the records we want to include in the batch. In this example, we're going to merge all Accounts with the Type "Customer - Direct". First we query the data we need: mergeup__Template__c template = [ SELECT Id FROM mergeup__Template__c WHERE Name = 'Account Template' LIMIT 1 ]; Id templateId = template.Id; List<Account> accounts = [ SELECT Id FROM Account WHERE Type = 'Customer - Direct' ]; Next we create our CreateBatchWithRecordsParams object and initialize the templateId and records properties: mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams params = new mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams(); params.templateId = templateId; params.records = accounts; We can create as many batches at once as we would like by creating more of these params objects. For instance we might want to create another batch that handles Accounts that are of a different Type. For this example we'll stick to creating one batch.Also note that the records could be any sObject type, as long as it is the same type that the MergeUp Template is based on. Lastly, we use the param we created to build a new MergeUp Batch using the static method CreateBatchRecords_Invocable.createBatchWithRecords(). List<mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams> listOfParams = new List<mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams>(); listOfParams.add(params); List<mergeup.CreateBatch_Globals.CreateBatchResponse> responses = mergeup.CreateBatchRecords_Invocable.createBatchWithRecords(listOfParams); Here is the full example code: mergeup__Template__c template = [ SELECT Id FROM mergeup__Template__c WHERE Name = 'Account Template' LIMIT 1 ]; Id templateId = template.Id; List<Account> accounts = [ SELECT Id FROM Account WHERE Type = 'Customer - Direct' ]; mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams params = new mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams(); params.templateId = templateId; params.records = accounts; List<mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams> listOfParams = new List<mergeup.CreateBatch_Globals.CreateBatchWithRecordsParams>(); listOfParams.add(params); List<mergeup.CreateBatch_Globals.CreateBatchResponse> responses = mergeup.CreateBatchRecords_Invocable.createBatchWithRecords(listOfParams); Notes The MergeUp Template used must use a Record type Template Group, and the Object type of the Template Group must match the Object type of the sObjects that are passed in.One of the things you could do using the mergeBatchId from CreateBatch_Globals.CreateBatchResponse is start the MergeUp Batch by changing its status to Started. For more information on starting a MergeUp Batch, see Starting a MergeUp Batch. Next Steps Learn how to start your MergeUp Batch. Starting a MergeUp BatchStarting Multliple MergeUp BatchesScheduling a MergeUp Batch to Start Later Learn other ways to batch merge with MergeUp Generating Multiple Documents Based on a ReportGenerating Multiple Documents from a List of Ids in ApexGenerating Multiple Documents with Advanced Customization in Apex Classes and methods used in this walkthrough CreateBatch_Globals.CreateBatchWithRecordsParamsCreateBatch_Globals.CreateBatchResponseCreateBatchRecords_Invocable.createBatchWithRecords() TitleCreating a MergeUp Batch from a List of Records in ApexURL NameGenerating-Multiple-Documents-from-a-List-of-Records-in-Apex