Loading
Salesforce Document Generation
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Example for a Sample Flow to Generate and Merge Contract Documents

          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.
          1. Create a Screen flow.
            1. From Setup, enter Flows in the Quick Find box, and then select Flows.
            2. Click New Flow.
            3. Select Screen Flow, and click Create.
          2. Add a template selection screen.
            1. Add a Screen element and label it Select Template.
            2. Add input fields for document title and template selection.
            3. Configure Template Selection as a pick list component.
            4. 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.
            5. If needed, apply additional filters to display only specific templates based on your requirements.
          3. Generate and preview the document.
            1. Add a Screen element and label it Quote Document Generation.
            2. Drag the Generate Document component onto the screen.
            3. 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.
            4. Run the Screen Flow to generate and preview the document.
              The flow now transitions to file preparation for merging.
              Screen Flow diagram showing the initial structure with Start, followed by Select Template screen, and then Quote Document Generation screen where the Generate Document component is used.
          4. Add file selection for merge.
            1. 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.
            2. Configure the element to return files for merging.
            3. Add a Loop element to iterate through ContentDocumentLink records.
            4. Add an Assignment element to collect ContentVersion IDs.
            5. Add a Get Records element to retrieve Content Version records.
            6. Add a Screen element to show files in a selectable list.
            7. Add a Screen element with a Data Table component to display retrieved Contract files in a selectable list.
            8. Add a File UploadFile Upload component to upload additional files.
              Flow Builder diagram showing a loop through ContentDocumentLink records, assignment to collect ContentVersion IDs, a Get Records element for Content Version, and a screen with a file upload component for selecting and uploading files to merge.
          5. Prepare file IDs for merge.
            1. Add a Loop element to iterate through selected attachments.
            2. Add an Assignment element to collect selected file IDs.
            3. Add an Assignment to collect uploaded file IDs.
            4. 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;
                  }
              }
              
          6. Merge documents.
            1. Add a Screen element.
            2. Add the Generate Document component.
            3. Select Merge PDF as the Service Function.
            4. Pass the list of PDF file IDs to merge the documents.
          7. Save and activate the Screen Flow.
            Flow Builder diagram showing a loop to collect selected file IDs, assignments for selected and uploaded files, an Apex action to transform IDs into a string, a Merge Document screen, and the end of the flow.
          8. Create a custom action to open the Screen Flow.
            1. From Setup, go to Object Manager and select the Contract object.
            2. Click Buttons, Links, and Actions.
            3. Click New Action.
            4. Set Action Type to Flow and select the Screen Flow that you created.
            5. Enter a label and name, and then save the action.
            6. 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.
           
          Loading
          Salesforce Help | Article