Tutorials
Document Generation Tutorials
How to generate a document via Screen Flow?
in this tutorial, you’ll learn how to enable users to select a template via a screen flow on the salesforce record page to generate documents this approach gives end users the flexibility to choose from multiple templates at runtime rather than relying on a predefined one after creating and publishing a template (see the how to build a template (word) docid\ lrofcwxxmyxa9aiic it ) tutorial), you’ll configure salesforce flow to support document generation with multiple templates by using the generate document (batch) docid\ xahb umkpvgfm3k4ycbdp action this setup allows you to dynamically generate different types of documents, such as purchase orders, based on the template selected by the user in the screen flow what to expect by the end of this tutorial, you will know how to allow users to select document templates directly from a salesforce record page using a screen flow generate documents from one or more templates in a single process decide where generated documents are stored, either in salesforce or external storage tutorial video refer to the quick video if you would like to have a visual guide for this tutorial read on if you prefer textual content video will be added soon 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 doc gen templates ensure that the templates published for the object are listed under the document generation tab → docgen templates locate the template id for the template you want to use and note it down, as you’ll need it as an input in your flow action 1 create custom metadata types create custom metadata types to store details about your document templates, such as the object, template name, template id, and type (e g , pdf, docx) define the following fields in your custom metadata type template name object template id type you can copy the template id from the doc gen templates tab (of the cloudfiles app) and configure metadata types the type field is used to store the file extension ( pdf , docx , xlsx ) this value will be passed into the flow action to define the output document type, ensuring that the generated file matches the required format 3 build a screen flow in salesforce 1 choose screen flow navigate to setup → flows in salesforce click new flow → select screen flow 2 create a 'recordid' input variable create a recordid variable to capture the salesforce record id (order 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 a record page 3 add a 'decision' element add a decision element to check the object type before continuing in the flow in this example, configure the condition to check for the order object using the first 3 digits of the recordid this ensures that only logic related to purchase orders is processed in this branch of the flow for other objects (like account or contract), you can add additional outcomes to handle their templates separately 4 add a 'get records' element add a get records element to fetch available templates stored in custom metadata types, filtered by object type this ensures only the relevant templates are fetched for display on the screen 5 include a 'screen' include a screen element to present the fetched templates to users and the destination selection for storing the file add a data table add a data table component to the screen, so users can view and select one or more templates source collection → set the data table’s source collection to the output of your get records element {get templates} this ensures the table is populated with templates retrieved from salesforce columns → add relevant fields such as template name to clearly identify templates row selection mode > set to multiple add radio buttons add radio buttons to allow users to choose the destination where the generated document should be stored options → provide values such as salesforce → store the document in the notes & attachments section of the related record sharepoint / google drive → store the document in an external storage system 6 loop the templates and assign variables loop the selected templates of the data table create both individual and collection apex defined variables of the class cldfs generatedocumentflowinput cldfs generatedocumentflowinput inside the flow for each selected template, populate the required configuration values such as templateid , recordid , and documenttype using an assignment element within the current loop iteration 7 configure destination parameters include a decision element to configure destination parameters based on the user’s selection in the screen salesforce → assign values so the generated document is stored in the notes & attachments section of the record external storage (sharepoint, google drive, etc ) → assign the corresponding library, driveid, and folder id parameters use assignment elements for each outcome to map the correct destination details refer to the destination section in generate document docid\ qfdwhv81mxgwsly7nppsc for more details assign library, id, driveid parameters for external storage add the single variable {!docgeninput} to collection variable {!docgeninputcollection} 8 add the 'generate document (batch)' action finally, add the generate document (batch) docid\ xahb umkpvgfm3k4ycbdp action to your flow pass the collection variable {!docgeninputcollection} into the inputs parameter this allows salesforce to generate documents for all selected templates in one go the action ensures that each template, along with its recordid, type, and destination, is processed together 4 activate the flow save and activate the flow add flow to record page to allow users to generate documents directly from a record page, add the flow as a component navigate to the object manager in salesforce setup select the order object go to lightning record pages → edit the desired page drag the flow component onto the page layout in the properties panel, select your newly activated flow save and activate the page users will now see a flow section on the record page to generate documents by selecting templates and destination testing once the flow is added to the order record page, it will now display the screen with available templates select the required templates choose a destination (salesforce) run the flow the generated document(s) will then be saved in the order's notes & attachments 5 output parameters asynchronous document generation each document generation request (individual input) publishes a document generated docid\ mmtfv4zzddn2tgrwgszbu event upon successful processing use these events to trigger downstream automations or flows, such as notifications or additional processing adapting the flow for other objects this flow can also be adapted for other records by configuring step 3 – add a decision element update the decision outcomes to match the specific object (similar to how it was done for order ), ensuring that each object routes to its relevant set of templates