Signature
global static void execute( List<MergeDocumentsAsync.FlowInput> inputs )
| Parameter | Description | |
|---|---|---|
inputsList<MergeDocumentsAsync.FlowInput> |
required | One entry per merge. See the field breakdown below. |
FlowInput
| Parameter | Description | |
|---|---|---|
resourcesList<Resource> |
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. |
fileNameString |
required | The name of the merged PDF, up to 255 characters. .pdf is added when it is missing. |
destination |
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. |
contextString |
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. ItsDocumentMergeddetails carryFileandDestinationwhen the merge had a destination,ContentVersionIdwhen it did not, and yourContext. - A merge that failed publishes an Error Event with
ErrorEventTypeset todocument-merged.ErrorReasonsays what went wrong, andDocumentMergedErrorcarries theDestinationand yourContext.
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
// 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 } );
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.
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.”
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.