Method: generateDocsInBatch
Why?
Use generateDocsInBatch when you need to automate document generation across a collection of records (e.g., generating monthly statements for 500 Accounts). This method offloads the processing to the Salesforce Apex Flex Queue, preventing "Timeout" or "CPU Limit" errors that occur when trying to generate multiple files in a single synchronous transaction.
What: Technical Signature
This is an asynchronous method that initiates a background job.
public static Id generateDocsInBatch(List<Id> recordIds, Set<Id> templateIds, Integer batchSize, List<SDOC.BatchAction> actions)recordIds
List<Id>
Yes
The IDs of the base records to process.
templateIds
Set<Id>
Yes
The ID(s) of the templates to generate for each record.
batchSize
Integer
No
Number of records processed per batch (Default: 5). Max is typically 10-20 depending on template complexity.
actions
List
No
A list of SDOC.BatchAction objects (e.g., for merging files).
Return Value Returns a Salesforce Job ID (Id). You can use this ID to query the AsyncApexJob table to monitor the status of the generation.
Solve: Implementation Patterns
Scenario A: Simple Bulk Generation
Generate a standard PDF for a list of Opportunities identified in a query.
List<Opportunity> opps = [SELECT Id FROM Opportunity WHERE StageName = 'Closed Won'];
List<Id> oppIds = new List<Id>(new Map<Id, Opportunity>(opps).keySet());
Set<Id> templateIds = new Set<Id>{'00Xxxxxxxxxxxxx'};
// Execute batch
Id jobId = SDOC.DocumentSDK.generateDocsInBatch(oppIds, templateIds, 5, null);
System.debug('Batch Job Started: ' + jobId);Scenario B: Generating a Single Merged PDF (The "Packet" Pattern)
Generate invoices for 10 Accounts, but merge them into one single PDF file for easy printing.
⚠️ Constraints & Limitations
Trigger Context: Like all SDK methods, this cannot be called directly from a Trigger. Use a Queueable or Platform Event to initiate the batch.
Document Actions: S-Docs "Document Actions" (configured at the template level) are not supported within the Batch SDK. If your template has post-generation actions, they will not fire.
Format Support: Supports PDF and MSX (Word/Excel) formats.
Governor Limits: * If you encounter
Too many SOQL queries: 201, reduce yourbatchSize.If you encounter
Heap Size Too Large, reduce the number of records in therecordIdslist or simplify the template.
Last updated
Was this helpful?

