You are here:
Example for a Sample Flow to Generate and Merge Contract Documents
Create a Screen Flow that generates a contract document, previews it, and optionally merges it with additional PDF files.
Required Editions
| Available in: Lightning Experience |
| Available in: Professional, Enterprise, Unlimited, and Developer Editions |
| User Permissions Needed | |
|---|---|
| To use the Generate Document LWC from a Screen Flow: | CLM Admin User OR DocGen Designer |
Before you Begin
- Configure document templates and data mappings for contracts.
- Make sure that the Generate Document component is available in Flow.
- Verify access to the Contract object and related files.
- Make sure that the Doc Generation API Version Type is set to Advanced in Document Generation Settings to support all token types.
In the sample Flow, the Generate Document component is used in two stages.
- On the first screen, the component generates the document by using the selected template and shows a preview.
- On the second screen, the same component runs again to merge the generated document with additional PDF files.
-
Create a Screen flow.
- From Setup, enter Flows in the Quick Find box, and then select Flows.
- Click New Flow.
- Select Screen Flow, and click Create.
-
Add a template selection screen.
- Add a Screen element and label it Select Template.
- Add input fields for document title and template selection.
- Configure Template Selection as a pick list component.
-
Create and assign a Record Choice Set for template selection.
- Map Object to Document Template.
- Map condition Is Active equals True.
- Map Store value to Template ID.
- Map Display Value to Template Name.
- If needed, apply additional filters to display only specific templates based on your requirements.
-
Generate and preview the document.
- Add a Screen element and label it Quote Document Generation.
- Drag the Generate Document component onto the screen.
-
Configure the component.
- Map ID or Record ID to the Contract record ID. If the component is used on a Contract record page, the Record ID is automatically available.
- Map Template ID from the previous screen.
- Map Document Title.
- Set Service Function to generateAndConvert or generateDocument.
-
Run the Screen Flow to generate and preview the document.
The flow now transitions to file preparation for merging.
-
Add file selection for merge.
-
Add a Get Records element to retrieve ContentDocumentLink records related to the Contract
record.
- Map Object to ContentDocumentLink.
- Map Condition to LinkedEntityId equals Contract Record ID.
- Configure the element to return files for merging.
- Add a Loop element to iterate through ContentDocumentLink records.
- Add an Assignment element to collect ContentVersion IDs.
- Add a Get Records element to retrieve Content Version records.
- Add a Screen element to show files in a selectable list.
- Add a Screen element with a Data Table component to display retrieved Contract files in a selectable list.
-
Add a File UploadFile Upload component to upload additional
files.
-
Add a Get Records element to retrieve ContentDocumentLink records related to the Contract
record.
-
Prepare file IDs for merge.
- Add a Loop element to iterate through selected attachments.
- Add an Assignment element to collect selected file IDs.
- Add an Assignment to collect uploaded file IDs.
-
Add an Apex Action to transform the list of IDs into a string.
public with sharing class FlowCollectionUtils { @InvocableMethod( label='Convert Text Collection to Comma String' description='Takes a Text Collection (like var_filesToAttach) and returns a single comma-separated string.' ) public static List<String> convertIdsToString(List<List<String>> inputIds) { List<String> results = new List<String>(); // Flow sends a List of Lists. We loop through the outer list. for (List<String> idList : inputIds) { if (idList != null && !idList.isEmpty()) { // String.join takes the collection and adds commas automatically results.add(String.join(idList, ',')); } else { // Return an empty string if the collection was empty results.add(''); } } return results; } }
-
Merge documents.
- Add a Screen element.
- Add the Generate Document component.
- Select Merge PDF as the Service Function.
- Pass the list of PDF file IDs to merge the documents.
-
Save and activate the Screen Flow.
-
Create a custom action to open the Screen Flow.
- From Setup, go to Object Manager and select the Contract object.
- Click Buttons, Links, and Actions.
- Click New Action.
- Set Action Type to Flow and select the Screen Flow that you created.
- Enter a label and name, and then save the action.
-
Add the action to the page layout.
Generate a contract document, preview it, and optionally merge it with additional PDF files. The Flow attaches the final merged document to the contract record.
Did this article solve your issue?
Let us know so we can improve!

