# Merge Documents (Async)

Merge files into one PDF in the background from Apex, with each result delivered as a CloudFiles Event.

> Kind: Apex method · Updated: 2026-11-25

Use this method to merge files into one PDF in the background from your own Apex code. It is the Apex form of the Merge Documents (Async) Flow action: you call the action's class directly. Each input is one merge. The method returns straight away, and each merge's result arrives later as a CloudFiles Event. When you need the merged file in the same transaction, use Merge Documents (Sync) instead.

## Signature

```apex
global static void execute(
  List<MergeDocumentsAsync.FlowInput> inputs
)
```

### Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| inputs | List&lt;MergeDocumentsAsync.FlowInput&gt; | required | One entry per merge. See the field breakdown below. |

### FlowInput

### FlowInput
| Parameter | Type | Required | Description |
|---|---|---|---|
| resources | List&lt;<a href="#/apex-type-resource">Resource</a>&gt; | required | The files to merge, in the order they should appear in the PDF. 1 to 50 files, each with an id and a library, plus a driveId for a file in external storage. For a Salesforce File, id is its ContentDocumentId and library is salesforce. |
| fileName | String | required | The name of the merged PDF, up to 255 characters. .pdf is added when it is missing. |
| destination | <a href="#/apex-type-resource">Resource</a> | optional | Where to save the merged PDF. For a Salesforce record, set library to salesforce and id to the record Id. For external storage, set library, driveId and the folder's id. Leave it empty to save the PDF to Salesforce Files. |
| context | String | optional | Any text, passed back in this merge's Document Merged event or Error Event. Use it to tell which merge an event belongs to, for example the Id of the record you merged for. |

## Return Type

void. Each merge's result is published as a CloudFiles Event, which you read with Get Event Details:

- A merge that worked publishes a Document Merged event, of type document-merged. Its DocumentMerged details carry File and Destination when the merge had a destination, ContentVersionId when it did not, and your Context.
- A merge that failed publishes an Error Event with ErrorEventType set to document-merged. ErrorReason says what went wrong, and DocumentMergedError carries the Destination and your Context.

An input with a problem, such as a file without an id or library, more than 50 files, or a blank fileName, does not throw. It is not sent to CloudFiles, and it gets an Error Event whose reason starts with “The merge was not sent”. The other inputs still go ahead.

## Example

```apex
// The files to merge, in the order they should appear
List<cldfs.Resource> files = new List<cldfs.Resource>{
    new cldfs.Resource('069dL00000A4vd3QAB', 'file', null, 'salesforce', null),
    new cldfs.Resource('069dL00000A4vd4QAB', 'file', null, 'salesforce', null)
};

// Save the merged PDF on the Opportunity
cldfs.Resource destination = new cldfs.Resource('006dL000005H4srQAC', null, null, 'salesforce', null);

cldfs.MergeDocumentsAsync.FlowInput input = new cldfs.MergeDocumentsAsync.FlowInput();
input.resources = files;
input.fileName = 'Closing Packet';
input.destination = destination;
input.context = '006dL000005H4srQAC';

cldfs.MergeDocumentsAsync.execute(
    new List<cldfs.MergeDocumentsAsync.FlowInput>{ input }
);
```

> **No callout in your transaction** — The method queues the merges and returns. CloudFiles is called from a background job after your transaction, so you can call this method after DML and from a trigger. The merge runs as the CloudFiles integration user, so the libraries it reads from and saves to must be connected for the org, and the org needs a CloudFiles Document Generation license.

> **Call it once with every merge** — Put all your merges in one call rather than calling the method once per record. From Batch Apex, a future method or a Queueable, the method starts a Queueable, and Salesforce allows only one per transaction there, so a second call throws: “Merge Documents (Async) was called too many times in one transaction. Call it once for many records rather than in a loop.”

> **Limits** — Up to 50 files in one merge, and up to about 400 MB and 50,000 pages. PDF, Word, PowerPoint and image files can be merged; everything that is not a PDF is converted to PDF first.
