# Batching & Combining Documents

#### Why?

A common business requirement is the "Bulk Print" or "Monthly Statement" run. Generating 500 individual PDFs is useful for digital records, but if those documents need to be printed and mailed, or sent as a single legal packet, the user needs them merged into one continuous file.

The Batch SDK allows you to automate the generation of multiple records while the CombinedDocumentHandler handles the heavy lifting of merging those files into a single PDF.

#### What: The "Handler" Pattern

When using `generateDocsInBatch`, you can pass a list of BatchActions. The `SDOC.CombinedDocumentHandler` is a specialized action that listens for the completion of each document in the batch and appends it to a "Master" PDF.

***

#### Solve: Implementation Steps

**1. Identify the Target Records**

Collect the IDs of the records you wish to process.

Java

```
List<Account> overdueAccounts = [SELECT Id FROM Account WHERE Status__c = 'Overdue'];
List<Id> accountIds = new List<Id>();
for(Account acc : overdueAccounts) {
    accountIds.add(acc.Id);
}
```

**2. Configure the Combined Handler**

Instantiate the handler and define your output settings.

Java

```
// Define the merge logic
SDOC.CombinedDocumentHandler myHandler = new SDOC.CombinedDocumentHandler();
myHandler.fileName = 'Monthly_Invoices_Batch_' + Date.today().format(); // No extension
```

**3. Execute the Batch**

Pass the handler into a list of `BatchActions` and invoke the SDK.

Java

```
// Prepare the action list
List<SDOC.BatchAction> actions = new List<SDOC.BatchAction>{ myHandler };

// Execute: (Record IDs, Template IDs, Batch Size, Actions)
Id jobId = SDOC.DocumentSDK.generateDocsInBatch(
    accountIds, 
    new Set<Id>{'00Xxxxxxxxxxxxx'}, 
    5, 
    actions
);
```

***

#### Technical Behavior

* Output Location: By default, the combined PDF is attached to the first record in the `accountIds` list.
* Large Batches: If the combined file becomes too large (exceeding Salesforce file limits), the SDK will automatically "split" the output into multiple combined files (e.g., *Part 1*, *Part 2*).
* Execution: This process is asynchronous. You can monitor progress via Setup > Apex Jobs.

***

#### ⚠️ Constraints & Best Practices

* PDF Only: The `CombinedDocumentHandler` currently only supports merging PDF and PDF-UPLOAD templates.
* Naming Conventions: Ensure your `fileName` does not contain special characters or file extensions (like `.pdf`), as the SDK appends the extension automatically.
* Heap Limits: Merging 500+ pages can occasionally hit Apex Heap limits. If you encounter failures on very large runs, try reducing the number of records in the initial `List<Id>`.
* Template Consistency: For the best visual results, ensure all templates being merged share the same page size (e.g., all Letter or all A4).


---

# 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/implementation-solves/batching-and-combining-documents.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.
