# Create file

## Overview

The Create File action is a powerful automation utility that bridges the gap between Flow data and Salesforce Files. It eliminates the need to manually navigate the complex relationship between `ContentVersion`, `ContentDocument`, and `ContentDocumentLink`.

{% hint style="warning" %}

#### Note: What this action is NOT

This is not a "Document Builder" with a visual editor. It creates a file based exactly on the data you provide.

* **For CSV/TXT**: Great for raw data exports.
* **For PDF**: It will create a PDF containing the exact text/HTML you provide, but it does not support "Drag & Drop" styling
  {% endhint %}

### Key Capabilities

* **Dynamic Document Generation**: Instantly turn user screen inputs, record fields, or data collections into downloadable documents.
* **Smart Linking**: Automatically attaches the created file to any record (Opportunity, Case, Custom Object, etc.) via the **Related Record ID**, making it immediately visible in the record's "Files" related list.
* **Format Agnostic**: Create anything from simple **Log files (.txt)** and **Data exports (.csv)** to complex **Documents (.pdf)** or **Images**, provided the source data is Base64 encoded.
* **Clean Output**: Returns the new **File ID** immediately, allowing you to use the created file in subsequent Flow steps, such as sending it in an email or posting it to Chatter

***

### Input Parameters

| Parameter             | Requirement | Description                                                              |
| --------------------- | ----------- | ------------------------------------------------------------------------ |
| **Base64 Data**       | *Required*  | The core content of your file, encoded in Base64 string format.          |
| **File Name**         | *Required*  | The name of the file including the extension (e.g., `Invoice_001.pdf`).  |
| **Description**       | Optional    | A brief summary of the file's content.                                   |
| **Related Record Id** | Optional    | The ID of the record (e.g., `006...`) where the file should be attached. |

***

## How to Prepare Your Data (Visual Example)

The most common question is: "How do I get my Salesforce data into the Base64 Data field?" Because Salesforce stores files as binary data, you cannot simply plug a "Text" variable into this action. You must first encode your content.

### 1. Turning Text into a File (CSV/TXT)

If you want to create a file from record data or user input, follow this process:

* **Step A: Gather your content**. Use a Loop and an Assignment (or a Text Template) to build your file's body. For a CSV, this would be a list of values separated by commas.
* **Step B: Encode to Base64**. Since Flow doesn't have a native "Encode" button, you must use a helper.
  * **Community Solution**: Use a free Invocable Action (like *Convert Text to Base64* from UnofficialSF).
  * **Apex Solution**: Use a simple one-line Apex method: `EncodingUtil.base64Encode(Blob.valueOf(yourText))`.
* **Step C**: Map to Action. Pass the Output of that encoding step directly into the Base64 Data field of the Create File action.

### 2. The Logic Flow

Salesforce Data > Text Variable > Encoding Helper (Apex/Action) > Create file

{% hint style="warning" %}

#### Important

Why is this necessary? > If you skip the encoding step and pass raw text into the Base64 Data field, the action will create a file, but it will be corrupted or appear empty when you try to open it.
{% endhint %}

***

## Example

### Generating a CSV of Case Details

*Use this example to learn how to turn record data into an attached file.*

{% stepper %}
{% step %}

#### **Gather your Data**

Add a Get Records element to find the records you want to export.

* **Object**: Case
* **Filter**: `AccountId` equals `recordId` (The ID of the Account you are on).
  {% endstep %}

{% step %}

#### **Build the File Content**

You need to turn those records into a "CSV string."

* **Create a Text Variable**: Name it `varCSVContent`.
* **Loop**: Iterate through your Case collection.
* **Assignment (Inside Loop)**: Add the Case data to your variable.

  > Example: `varCSVContent` Add `{!Loop_Case.CaseNumber} , {!Loop_Case.Subject}` followed by a line break.
  > {% endstep %}

{% step %}

#### **The "Base64" Transformation**

The Create File action requires data in Base64 format. Since Flow doesn't do this natively, you have two options:

* **Option A (Recommended)**: Use a free community action like [UnofficialSF's "Convert Text to Base64"](https://unofficialsf.com/convert-text-to-base64-invocable-flow-action/).
* **Option B (Custom)**: Create a tiny Apex Action with one line of code:

  `return EncodingUtil.base64Encode(Blob.valueOf(inputText));`
  {% endstep %}

{% step %}

#### **Configure the Create File Action**

Map the output from Step 3 into the Create File parameters

| Field                 | Value to Enter                                                        |
| --------------------- | --------------------------------------------------------------------- |
| **Base64 Data**       | `{!OutputFromStep3}`                                                  |
| **File Name**         | `ClosedCases.csv` (Don't forget the .csv extension!)                  |
| **Related Record Id** | `{!recordId}` (The ID of the Account where the file will be attached) |
| {% endstep %}         |                                                                       |

{% step %}

#### **Result**

When the Flow runs:

1. It collects the Cases.
2. It turns them into a text list.
3. It encodes that list into Base64.
4. Create File "re-hydrates" that code into a physical CSV file attached to your Account
   {% endstep %}
   {% endstepper %}

***

### Best Practices

* **Extensions Matter**: Salesforce determines the file type by the extension you provide in the File Name field. Always include `.pdf`, `.csv`, or `.txt`.
* **Size Limits**: Keep in mind that Apex actions have heap size limits. For extremely large files (multi-MB), ensure your Flow logic is optimized.
* **Privacy**: If the file contains sensitive info, use the "Show Advanced Options" in the Flow Builder to set the visibility or sharing privacy of the created file.

***

### Output Resources

Once the action runs, it returns:

* fileId: The ID of the created `ContentVersion`.
* fileName: The final name of the file.
* fileType: The detected file format.

You can use these outputs later in your flow, for example, to post the file to a Chatter feed or display a "Download" link on a success screen.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.avonnicomponents.com/flow/actions/data-manipulation-and-transformation/create-file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
