Tutorials
Document Generation Tutorials
How to Merge documents?
in this tutorial, you will learn how to merge multiple files into a single pdf using the merge documents (sync) action this feature allows you to combine various document types such as pdfs, docx, images (png and jpeg) into one consolidated pdf file what to expect by following this tutorial, you will learn how to combine different files into a single merged pdf configure salesforce flow to dynamically collect files and pass them into the merge documents (sync) action attach the final merged pdf to a destination prerequisites install cloudfiles in salesforce cloudfiles is available on the salesforce appexchange and can be installed using the standard app installation process check out installation docid\ aw06gffsiiq qdp1fkgcj and configuration docid\ zgbv7ll pzzzopel4 7up guides to set up your account and user access get started with setup in this tutorial, we’ll use the opportunity object imagine a scenario where users upload supporting files such as invoices (pdfs) , contracts (docx/jpeg) , or other documents to an opportunity record we’ll create a flow that can be launched from a button, allowing users to select these files and merge them into a single pdf that can be viewed directly from the opportunity record 1 create screen flow go to setup → flows in salesforce and create a new screen flow create a recordid variable to capture the salesforce record id (opportunity record) from which the flow is launched set its data type to text and mark it as available for input so the record id is automatically passed when the flow runs from the record page 2 collect content document ids add a get records element to fetch all contentdocumentlink records where the linked entity id equals the recordid this ensures you retrieve all files that have been uploaded to the opportunity add a loop element to iterate over each contentdocumentlink record fetched in the previous step create a variable resource named contentdocumentids , set its data type to text , and enable allow multiple values (collection) so it can store multiple ids this variable will hold all the document ids retrieved from the opportunity record, allowing them to be used later in the flow for merging inside the loop, use an assignment element to add each file’s contentdocumentid to the contentdocumentids collection variable 3 get content documents add a get records element to fetch all contentdocument records using the contentdocumentids 4 let users select files add a screen element to choose which files to merge, rather than merging all this provides flexibility for users to pick specific files add a screen element with a data table component set the source collection to the list of content documents contentdocumentids retrieved in the above step set row selection mode to multiple so users can pick more than one file add a loop element to iterate over the selected rows from the data table {!selectdocs selectedrows} to pass documents into the merge documents action, you need apex defined variables of type cldfs resource selectedresourcesingle → single resource (to configure each resource inside the loop) selectedresources → collection resource (to store all selected documents) assign values in loop inside the loop selected content documents , add an assignment element set selectedresourcesingle library = salesforce set selectedresourcesingle id = contentdocumentid ({ !loop selected content documents id} ) add to collection use another assignment element to add the selectedresourcesingle into the selectedresources collection 5 define destination defining a destination ensures that the merged document is properly linked and stored in salesforce to do this, create apex defined variable of apex class cldfs resource and define values such as id, driveid and library according to the storage location to save in salesforce, add an assignment element to set the values destination → id = recordid (ensures the merged file is linked to the current record) destination → library = salesforce (stores the file in the salesforce library) to store in external storage when using cloudfiles with the document management & sharing docid 3gglz90lgx5zfwkrgm7ra , assign the library (external library name), driveid (id of the connected drive), and id (folder or location id within the drive) 6 add the merge documents action the merge documents (sync) docid\ akbaey1ytn5miwrzxe1tl action is the final step that combines all the selected files into a single pdf in this step, you define where the file should be stored (destination), give it a name, and pass in the list of documents to merge drag in the merge documents (sync) flow action and configure the following input values destination → use the destination variable you created earlier to define where the merged file should be stored (e g , linked to the current record) file name → provide a name for the merged output file (e g , merged file ) resources → map the selectedresources collection variable, which contains all the documents to merge 7 add the flow to a button to make the flow easily accessible to users, add it as a button on the opportunity record page go to setup → object manager → opportunity → buttons, links, and actions, then create a new action of type flow and select the screen flow you created give the action a label, such as merge documents , and save it add the button to the opportunity page layout or lightning record page users will now see the merge documents button on the opportunity record, allowing them to select and merge files directly from the record note you can repeat the same setup for other salesforce objects such as accounts, cases, or custom objects where users upload files, making the merge functionality available across different processes test your setup go to an opportunity record upload files (e g , invoices, contracts, images) to the record click the merge documents button select the files you want to merge click next → wait for the merge to complete the merged pdf will be created and added to the opportunity record output parameters the merge documents (sync) action returns immediate outputs document metadata details of the generated file if saved in salesforce includes the file’s content document id and library (salesforce) if saved externally includes metadata (id, library, driveid, type, path of the file in external storage content version id the content version id of the generated file (when saved in salesforce) document name the name of the merged file download url a dynamic link to download the generated file preview url a dynamic link to preview the file this can be displayed as a clickable hyperlink in a screen or embedded in a custom screen component