# 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.

```java
public static Id generateDocsInBatch(List<Id> recordIds, Set<Id> templateIds, Integer batchSize, List<SDOC.BatchAction> actions)
```

Parameters | Parameter | Type | Required | Description | | :--- | :--- | :--- | :--- | | `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.

```java
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.

```java
// 1. Define the Merge Handler
SDOC.CombinedDocumentHandler mergeHandler = new SDOC.CombinedDocumentHandler();
mergeHandler.fileName = 'Monthly_Invoices_Combined'; // Do not include extension

// 2. Add to Action List
List<SDOC.BatchAction> actions = new List<SDOC.BatchAction>{ mergeHandler };

// 3. Execute
SDOC.DocumentSDK.generateDocsInBatch(accountIds, templateIds, 5, actions);
```

***

### ⚠️ 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 your `batchSize`.
  * If you encounter `Heap Size Too Large`, reduce the number of records in the `recordIds` list or simplify the template.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation/method-generatedocsinbatch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
