---
title: "Generate Document via SignNow"
url: "https://docs.signnow.com/docs/apex-generate-document"
type: "page"
section: "Integrations"
slug: "apex-generate-document"
---

# Generate Document via SignNow

# Apex action: Generate Document via SignNow

<!-- theme: info -->
> Before configuring this Apex action, review the [prerequisites](/docs/salesforce-apex-actions#prerequisites) for SignNow Apex actions and complete the required setup steps.

The **Generate Document via SignNow** action generates a document by merging a .docx template with Salesforce record data. This action runs synchronously and returns the **Document Group ID**, which you can use in subsequent flow steps.

<!-- theme: info -->
> This action reads its .docx file from Salesforce Files and requires a File ID. It does not accept a document generation template stored in SignNow Admin Tools: to send one of those, use the [Send SignNow Template](/docs/apex-send-template) action. For an overview of all document generation paths in Salesforce, see [Document generation](/docs/salesforce-document-generation).

Use this action when the generated document needs to be processed further before or instead of sending it for signature. Because the action returns the **Document Group ID**, you can pass it to any subsequent [SignNow Apex action](/docs/salesforce-apex-actions), for example, **Get SignNow Send Invite URL** to let the user review and send manually, **Send SignNow Freeform Invite** to send automatically, or **Create SignNow Embedded Invite** to embed signing into your Salesforce page.

For example, let's configure a screen flow that generates a sales invoice from an Opportunity and opens a send link so the user can review and send the document for signature. The flow is launched from a button on the Opportunity record page. An input variable receives the current record and passes the Opportunity ID to the Generate Document action.

---

## Step 1. Create a .docx template with tags

Prepare a .docx file as the document template. Tags in the template are placeholders that are replaced with Salesforce field values when the document is generated.

For full instructions on creating the template, finding Salesforce API field names, and tag syntax, see **Step 1** in [Apex action: Generate and Send Document via SignNow](/docs/apex-generate-and-send-document#step-1-create-a-docx-template-with-tags).

**Example of a .docx invoice template with tags**

<!--
focus: center
bg: none
-->
![docgen_docx_template_tags.png](/reference-assets/images/SignNow_Salesforce/salesforce_docgen/docgen_docx_template_tags.png)


---

## Step 2. Upload the .docx template to Salesforce Files

Upload the template to Salesforce Files and retrieve the file ID.

For instructions, see **Step 2** in [Apex action: Generate and Send Document via SignNow](/docs/apex-generate-and-send-document#step-2-upload-the-docx-template-to-salesforce-files).

---

## Step 3. Configure the screen flow

Go to **Flows > New Flow**.

Select **Start From Scratch**. Click **Next**.

Select **Screen Flow**. Click **Create**.

### Create the recordId variable

The screen flow requires an input variable to receive the Opportunity record when launched from a button.

- Click **New Resource** in the Toolbox.
- Select **Resource Type > Variable**.
- Set **API Name** to `recordId`. Steps 4 and 5 reference this variable by name. Use exactly `recordId`.
- Set **Data Type** to **Record**.
- Set **Object** to **Opportunity**.
- Under **Availability Outside the Flow**, check **Available for input**.
- Click **Done**.

![apex_docgen_simple_variable.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_variable.png)

When the flow is launched from a button on an Opportunity record, Salesforce automatically passes the current record into this variable.

### Create the redirectUrl formula

See [Formula Resource](https://help.salesforce.com/s/articleView?id=sf.flow_ref_resources_formula.htm&type=5) in Salesforce Help for full details on formula resources in Flow Builder.

The **Get SignNow Send Invite URL** action accepts a redirect URL, the page users are taken to after they finish working with the document in SignNow. This can be any URL: a Salesforce record page, a custom confirmation page, or any other destination. Create a formula resource for this.

In our example, we redirect the user back to the Opportunity record.

- Click **New Resource** in the Toolbox.
- Select **Resource Type > Formula**.
- Set **API Name** to `redirectUrl`. Step 5 references this resource by name.
- Set **Data Type** to **Text**.
- In the **Formula** field, enter your formula:

```
"https://[your-org-domain]/lightning/r/Opportunity/"&{!recordId.Id}&"/view"
```

Replace `[your-org-domain]` with your Salesforce org domain (e.g., `acme43-dev-ed.develop.lightning.force.com`). Use `&` for string concatenation. `{!recordId.Id}` references the **Id** field from the `recordId` record variable; add it via **Insert a resource** to avoid typos.

- Click **Done**.

![apex_docgen_simple_formula.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_formula.png)

---

## Step 4. Configure the Generate Document via SignNow action

Click the **+** icon to add an action to the flow.

Start typing *signnow* in the input field and select **Generate Document via SignNow**.

Configure the action inputs:

- **File ID:** the ID of the .docx template retrieved in Step 2.
- **Record ID:** select the variable created in Step 3: **recordId > Opportunity ID**.
- (Optional) **SignNow Document Flow ID:** the ID of the pre-configured [SignNow document flow](/docs/document-flows) that defines recipients and the signing scenario. You can set it here or in the next step.

<!-- theme: info -->
> Copy the SignNow document flow ID from **SignNow Admin Tools > Document Flows**.

<!-- theme: warning -->
> **Important:** The roles configured in the document flow must match the role names in the .docx template's fillable field tags. For example, if the template uses `o:"Manager"`, the document flow must have a role named **Manager**.

- **SOQL Query:** a SOQL query that fetches all fields referenced in the template. For the rules on building the query, see **Step 5** in [Apex action: Generate and Send Document via SignNow](/docs/apex-generate-and-send-document#step-5-build-the-soql-query). For the invoice example:

```sql
SELECT
    Account.Name,
    Account.BillingStreet,
    Account.BillingCity,
    Account.BillingState,
    Account.BillingPostalCode,
    Account.BillingCountry,
    OrderNumber__c,
    Owner.Name,
    Owner.Email,
    (SELECT Name, Quantity, UnitPrice, TotalPrice FROM OpportunityLineItems)
FROM Opportunity
WHERE Id = <record_id>
```

![apex_docgen_simple_gen_action.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_gen_action.png)


The action returns the **SignNow Document Group ID**, used in the next step.

---

## Step 5. Configure the Get SignNow Send Invite URL action

After document generation, use this action to get a URL that opens the document in SignNow for sending.

Click the **+** icon and select **Get SignNow Send Invite URL**.

Configure the action inputs:

- **Redirect URL:** select the `redirectUrl` formula resource created in Step 3.
- **SignNow Document Group ID:** select **Outputs from Generate Document via SignNow > SignNow Document Group ID**.
- (Optional) **Record ID:** the Salesforce record ID associated with this document. Select **recordId > Opportunity ID**.
- (Optional) **SignNow Document Flow ID:** the ID of the SignNow document flow. If not set in the Generate Document action, you can set it here.
- **Type:** controls what the generated URL opens. Available values:
  - `manage`: opens the Add documents to document group page.
  - `edit`: opens the document in the editor, allowing the sender to add fields and send the invite.
  - `send-invite`: opens the Send Invite page.

  For our example, enter `edit`.

![apex_docgen_simple_url_action.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_url_action.png)


The action returns the **SignNow Send Invite URL**.

---

## Step 6. Configure the Open a Page action

Use the Salesforce **Open a Page** core action to open the send invite URL in the user's browser.

Click the **+** icon, select **Action**, then go to **Navigation Actions > Open a Page**.

Configure the action inputs:

- **Page Type:** External Web Page.
- **URL:** select **Outputs from Get SignNow Send Invite URL > SignNow Send Invite URL**.
- **Where to Open the Page:** New Browser Tab.

![apex_docgen_simple_page.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_page.png)

---

## (Optional) Add an error screen

To display an error message if document generation fails, add a **Screen** element on the fault path of the Generate Document action.

- Connect the fault path of the Generate Document action to a new **Screen** element.
- Add a **Display Text** component to the screen.
- In the component's text area, click **Insert a resource** and select **Outputs from Generate Document via SignNow > Error**. This inserts `{!Generate_Document_via_SignNow.error}`, the **Error** output returned by the Generate Document via SignNow action when it fails.


![apex_docgen_simple_error.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_error.png)

This ensures users see a meaningful message instead of the flow silently ending.

---

## Activate the flow and add a button

Once the configuration is ready, click **Save**, then **Activate**.

To launch the flow from an Opportunity record, add it as a button to the page layout. See [Add an Apex Action to a Page Layout](/docs/add-action-page-layout) for instructions.

---

## Salesforce Classic support

The **Generate Document via SignNow** action is fully supported in Salesforce Classic. The screen flow described in this guide runs in Classic without limitations. Two things behave differently in Classic and require the workarounds below.

### Invites Status widget in Classic

The SignNow Invites Status widget, used to track the status of generated documents sent for signature, is built as a Lightning Web Component (LWC) and is only available in Lightning Experience. To track invite statuses in Classic, embed the widget through a Visualforce page. For step-by-step instructions, see [Add the widget in Salesforce Classic](/docs/salesforce-invites-status-widget#add-the-widget-in-salesforce-classic).

### Launch the screen flow from a record page in Classic

The screen flow in this guide is launched from a button on a record page. In Lightning Experience, you add it as an Apex action (see [Activate the flow and add a button](/docs/apex-generate-document#activate-the-flow-and-add-a-button)). That approach does not apply in Salesforce Classic. Instead, launch the screen flow with a custom button that opens the flow URL directly.

**Create the custom button or link**

The flow URL includes the API name of your screen flow. To find it, go to **Setup > Flows** and open your flow. Copy the value from the **API Name** field on the flow detail page.

![apex_docgen_simple_classic_flow_name.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_classic_flow_name.png)

Then create the button:

- Go to **Setup > Object Manager** and select the object (for example, **Opportunity**).
- Click **Buttons, Links, and Actions**, then click **New Button or Link**.
- Set **Display Type** to **Detail Page Button**.
- Set **Behavior** to **Display in new window**.
- Set **Content Source** to **URL**.
- Enter the flow URL: `/flow/<flowApiName>?recordId={!Opportunity.Id}`, where `<flowApiName>` is the API name of your screen flow and `{!Opportunity.Id}` inserts the ID of the current record. Replace `Opportunity` with the object the button is placed on (for example, `{!Account.Id}`).
- Click **Save**.

![apex_docgen_simple_classic_custom_button.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_classic_custom_button.png)


**Add the button to the page layout**

- Select **Page Layouts** on the left panel and open the layout you want to edit.
- Locate the button under **Buttons** and drag it into the **Custom Buttons** section of the layout.
- Click **Save**.

![apex_docgen_simple_classic_layout.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_classic_layout.png)


**Add a finish screen to the flow**

In Classic, when a screen flow launched from a URL button reaches its end, Salesforce starts a new flow interview and shows the first screen again, so the flow appears to restart instead of stopping. To prevent this, add a screen that the flow lands on when it completes.

- In Flow Builder, add a **Screen** element as the last element of the flow, after the final **Open a Page** action.
- Add a **Display Text** component with a short confirmation message, for example: "Your document has been generated. You can close this window."
- Because the button opens the flow in a new window (**Behavior: Display in new window**), the user closes that window from this screen. The flow stops on the finish screen instead of restarting.

<!-- theme: info -->
> To close the window automatically instead of having the user close it, add a local action or custom component to the finish screen. A URL button on its own cannot close the window.

---

## Result

![apex_docgen_simple_result_edited.png](/reference-assets/images/SignNow_Salesforce/apex_docgen/apex_docgen_simple/apex_docgen_simple_result_edited.png)


Each time a user clicks the button on an Opportunity record, the flow generates a PDF from the .docx template, merges in the Opportunity data, and opens the document in the SignNow editor in a new browser tab. The user can review the document and send it for signature from there.

---

View the full list of [SignNow Apex actions](/docs/salesforce-apex-actions).


---
*Full page: https://docs.signnow.com/docs/apex-generate-document*
