Creating a MergeUp Batch with Advanced Customization in ApexLearn how to use Apex to create a MergeUp Batch with advanced customization features.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 using advanced features for the best customization possible. Steps To create a more customized batch, we will be using CreateBatchAdvanced_Invocable. This class allows each merge in a batch to have its own specific MergeUp Template, so to start we'll query a selection of templates instead of just one, and organize them by Name for easy access later. Map<String, Id> templateNameToId = new Map<String, Id>();for (mergeup__Template__c template : [ SELECT Id FROM mergeup__Template__c WHERE Name IN ('Account Template', 'EU Account Template', 'General Template') ]) { templateNameToId.put(template.Name, template.Id);} Next we create our CreateBatchAdvancedParams object, and set up the query for our Accounts, along with the basic structure: mergeup.CreateBatch_Globals.CreateBatchAdvancedParams params = new mergeup.CreateBatch_Globals.CreateBatchAdvancedParams();for (Account a : [ SELECT Id, Is_EU__c, Prefers_PDF__c FROM Account WHERE Type = 'Customer - Direct' ]) { mergeup.Merge_Parameters mParams = new mergeup.Merge_Parameters(); mParams.recordId = a.Id; mParams.templateId = templateNameToId.get('Account Template'); // set up other options here params.mergeParameters.add(mParams);} Now each Merge_Parameters object has a recordId and a templateId that it will use for the merge. If we don't have any more specific options to customize, these two are all we need when using a MergeUp Template that is part of a Record Template Group. But to show some advanced features, it's time to add customized logic to control options for each merge: mergeup.CreateBatch_Globals.CreateBatchAdvancedParams params = new mergeup.CreateBatch_Globals.CreateBatchAdvancedParams();for (Account a : [ SELECT Id, Is_EU__c, Prefers_PDF__c FROM Account WHERE Type = 'Customer - Direct' ]) { mergeup.Merge_Parameters mParams = new mergeup.Merge_Parameters(); mParams.recordId = a.Id; mParams.templateId = templateNameToId.get('Account Template'); // 1 if (a.Is_EU__c) { mParams.templateId = templateNameToId.get('EU Account Template'); } // 2 if (a.Prefers_PDF__c) { mParams.fileType = 'pdf'; } // 3 if (a.ParentId != null) { mParams.altUploadRecord = a.ParentId; } params.mergeParameters.add(mParams);} Here's what we are doing for the custom logic, labelled by number: We have a custom field in this example Is_EU__c, which is a checkbox that indicates whether an Account is from the EU. If it is, we want to use an alternate template, so we set the templateId to a different template from our map.We have another custom checkbox Prefers_PDF__c which indicates whether an Account wants all their documents in PDF form. If it is checked, we set the fileType to "pdf", which will override whatever file type is set on the MergeUp Template.If there is a parent Account on an Account, we want the merged document to be attached to the parent Account instead of the child, so we use altUploadRecord to set the parent Account's Id as the merged document destination. Using the CreateBatchAdvancedParams class, we can also add a merge for a template in a General Template Group. To do this, all we need is a Merge_Parameters object that takes the templateId of the template in the General Template Group. This Merge_Parameters does not need a recordId. All of the other parameters of Merge_Parameters still apply. For instance, let's override the fileType of this template and make it always merge as a Word document. mergeup.Merge_Parameters groupParams = new mergeup.Merge_Parameters();groupParams.templateId = templateNameToId.get('Group Template');groupParams.fileType = 'docx';params.mergeParameters.add(groupParams); Lastly, we use the param we created to build a new MergeUp Batch using CreateBatchAdvanced_Invocable.createBatchAdvanced(). List<mergeup.CreateBatch_Globals.CreateBatchAdvancedParams> listOfParams = new List<mergeup.CreateBatch_Globals.CreateBatchAdvancedParams>(); listOfParams.add(params); List<mergeup.CreateBatch_Globals.CreateBatchResponse> responses = mergeup.CreateBatchAdvanced_Invocable.createBatchAdvanced(listOfParams); Here is the full example: Map<String, Id> templateNameToId = new Map<String, Id>();for (mergeup__Template__c template : [ SELECT Id FROM mergeup__Template__c WHERE Name IN ('Account Template', 'EU Account Template') ]) { templateNameToId.put(template.Name, template.Id);}mergeup.CreateBatch_Globals.CreateBatchAdvancedParams params = new mergeup.CreateBatch_Globals.CreateBatchAdvancedParams();params.mergeParameters = new List<mergeup.Merge_Parameters>();for (Account a : [ SELECT Id, Is_EU__c, Prefers_PDF__c FROM Account WHERE Type = 'Customer - Direct' ]) { mergeup.Merge_Parameters mParams = new mergeup.Merge_Parameters(); mParams.recordId = a.Id; mParams.templateId = templateNameToId.get('Account Template'); if (a.Is_EU__c) { mParams.templateId = templateNameToId.get('EU Account Template'); } if (a.Prefers_PDF__c) { mParams.fileType = 'pdf'; } if (a.ParentId != null) { mParams.altUploadRecord = a.ParentId; } params.mergeParameters.add(mParams);}mergeup.Merge_Parameters groupParams = new mergeup.Merge_Parameters();groupParams.templateId = templateNameToId.get('Group Template');groupParams.fileType = 'docx';params.mergeParameters.add(groupParams);List<mergeup.CreateBatch_Globals.CreateBatchAdvancedParams> listOfParams = new List<mergeup.CreateBatch_Globals.CreateBatchAdvancedParams>(); listOfParams.add(params); List<mergeup.CreateBatch_Globals.CreateBatchResponse> responses = mergeup.CreateBatchAdvanced_Invocable.createBatchAdvanced(listOfParams); Notes When using a MergeUp Template from a Record type Template Group to create Merge_Parameters, a recordId must be set, and the Object type of the Template Group must match the Object type of the recordId.One of the things you could do using the mergeBatchId from 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 Records in ApexGenerating Multiple Documents from a List of Ids in Apex Classes and methods used in this walkthrough CreateBatchAdvanced_InvocableCreateBatch_Globals.CreateBatchAdvancedParamsMerge_ParametersCreateBatch_Globals.CreateBatchResponse TitleCreating a MergeUp Batch with Advanced Customization in ApexURL NameGenerating-Multiple-Documents-with-Advanced-Customization-in-Apex