> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/document-generation/method-generatedocsinbatch.md).

# 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)
```

| 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/document-generation/method-generatedocsinbatch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
