# Generate a Document in Flow Builder

Documents can be generated within flows using an Action element.

* Drag and drop a flow "Action" onto the flow canvas
* Select the "Document" category
* Select the "Generate Document" action


![This will appear if the image fails to load](../../assets/gifs/flows/flow_action_doc_gen.gif)

There are several configurable options for generating a document (see complete settings below), but 2 input variables are required:

1. Record ID - An input value passed into the flow.
2. Template ID - A ContentVersion record ID of a Word doc merge template.


### Record ID
The `Record ID` is typically passed into a screen flow from the record page layout. For auto-launched flows, the `Record ID` can be any Salesforce record to be merged with a document template.

![This will appear if the image fails to load](../../assets/gifs/flows/flow_recordid_input.png)


### Template ID
The `Template ID` is a reference to a `ContentVersion` file record that meets the following criteria
* Title = the title of the file template
* IsLatest = `{$GlobalConstant.True}`

The template must be a Word document merge template previously updated with tags mapped to merge fields. See the "Word Document Templates" documentation for details.

![This will appear if the image fails to load](../../assets/gifs/flows/get_contentversion_record.gif)

### Optional Input Values

|Input                              | Valid Values             | Default Value       |  Description            
|:----------------------------------|:--------------------------|---------------------|---------------|
|`Attach As`                        | File or Attachment       | Attachment          | Determines if the generated document is attached as a legacy Attachment or Chatter File|
|`Attach As New Version`| True or False       | False     | For AttachAs "Files", indicates that subsequent generations of the same record/template create a new File version. Matched based on file name.|
|`Content MIME Type`| `application/pdf` and `application/vnd. openxmlformats-officedocument. wordprocessingml.document` | `application/pdf` | This value is auto-set based on the `File Format` input.|
|`File Name`| Text(255) | GeneratedDocument.pdf | The Attachment Name or ContentVersion Title property.|
|`File Description`| Text(255) | empty | A description associated with the file|
|`File Format`| PDF or WORD | PDF | Determines the file format for generated document.|
|`Parent ID`| Any 15 or 18 char record ID | `Record ID` | Allows the generated document to be attached to a record other than the merge RecordID. Example: Merging a Contact record but attaching to parent Account record. |
|`Run Asynchronous`| True or False | False | Determines if the action is executed in a `@future` method context. Because this action makes an API callout, any uncommitted DML may throw an exception. Set `Run Asynchronous=True` to avoid this type of error.|


### Output Values

All document and room actions reflect back the input values to the output and additionally provide a boolean `Success` value and a `Message` value. It's generally best practice to capture these 2 values in variables, particularly in screen flows, for use in providing the user feedback.

|Output                              | Values             |Description            
|:----------------------------------|:--------------------------|---------------------|
|`Attachment ID`                    | Attachment ID or ContentVersion ID | Depending on the file format input, provides a reference to the document that was generated. This value is commonly mapped to a flowe variable for use in downstream activities, like sending the document via email |
|`Success`                    | True or False | Indicates whether the document generation action was successful. If `False` then details of the error are available in the `Message` output value. |
|`Message`                    | null or String value | Populated with error message details when `Success=False`. |
