What to Expect

By the end of this tutorial, you will know how to:

  1. Create a DOCX template with fields mapped from Salesforce
  2. Insert text, image, and table elements into a DOCX template
  3. Use conditions to show or hide content based on logic
  4. Preview and publishing document to Salesforce

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

Pre-requisites

Install CloudFiles in Salesforce

CloudFiles Document Generation app is available on the Salesforce AppExchange and can be installed using the standard app installation process. Check out Installation guide to know more.

Template Builder Setup

The CloudFiles add-in connects with your Salesforce Org to enable template creation using Salesforce data. It can be installed in Microsoft Word, Excel, or PowerPoint, allowing you to design templates directly. For installation steps, see Template Builder Setup.

Stages of Document Generation

Document Generation includes 3 main stages:

  • Build
  • Preview
  • Publish

After logging in to Salesforce through the CloudFiles add-in, set Salesforce as the platform and select the Order object. In this tutorial, we will use the Order object to create a sample Purchase Order template.

Why use a Purchase Order Template?

A purchase order is a common document used to confirm and track product or service purchases. It generally includes the order number, account information, shipping and billing details, and a list of items.

In this tutorial, we use the Order object in Salesforce, which contains all the required details for generating the document. For item-level information, we use the OrderItem object, which holds product names, quantities, prices, and totals used to populate tables in the template.

We have created a sample purchase order template with placeholder fields for reference (see screenshot below). In the Build stage, we will show how to use each action: Insert Field, Insert Image, Insert Table, Insert Link, and Insert Condition, to replace those placeholders with actual merge fields from Salesforce.

After installing the CloudFiles add-in and setting up Salesforce, you can use the template actions to insert fields and tables from Salesforce data into the document.

Stage 1 - Build

1 - Global variable

When building templates, you work with global variables that help you pull in Salesforce data. The most common one is $Record, which refers to the record you're generating the document for β€” for example, an Order in this case. Using this, you can easily bring in fields like OrderNumber, EffectiveDate, and related account fields.

Alongside this, there's also the $User variable. This refers to the Salesforce user who is generating the document. If you're using the document generation widget inside Salesforce, $user will correspond to whoever is running that action. In flows, it picks up the user who is in context at the time β€” so in a screen flow, it's the person interacting with the flow, and in a record-triggered flow, it's the person who made the record changes. This means you can insert fields not just about the record itself, but also about the user generating the document, such as their name or email address.

2 - Insert Field

Insert the following fields into the purchase order template:

  • OrderDate – {{!$Record.EffectiveDate}}
  • OrderNo – {{!$Record.OrderNumber}}
  • AccName – {{!$Record.Account.Name}}
  • BillingStreet – {{!$Record.Account.BillingStreet}}

You can similarly insert other fields such as BillingCity, ShippingStreet, ShippingCity, etc.

Additionally, you can insert fields from other related object records, such as Contract, by creating a Record Single Variable resource and then using template actions to reference and insert those fields into your document.

Note

Note: Ensure that the Integration User has the necessary permissions to access the fields being inserted. If the required access is missing, the fields will not be available for use in the template.

3 - Insert Picture

Use Insert Picture action to add an image to the template. This is ideally used for company logos, product images, etc. Place the cursor where the image should appear in the document, then select the Insert Picture action. Choose a field that contains a valid image URL, such as a logo URL stored in the Account record. You can also adjust the size of the image.

In the purchase order template, insert the custom field Company_Logo__c from the Account object, which holds the image URL.

  • Image - {!$Record.Account.Company_Logo__c}

4 - Insert Link

Use Insert Link to add clickable URLs from Salesforce fields into your template.

In the purchase order template, insert the field Website from the Account object, which holds the URL of the website.

  • Website – {{$Record.Account.Website}}

This will generate a working hyperlink in the final generated document.

5 - Insert Table

Use Insert Table action to include repeatable rows for lists, such as line items or products.

Before adding a table, create a Record Collection Variable with object set to 'Order Product' which is typically used to represent the line items associated with the order (e.g., products purchased, quantities, and pricing)and add filter Criteria as below.

  • Order ID Equals {!$Record.Id}

Now, the record collection OrderItems contains the products of the order.

To configure the table, follow the steps below:

  1. Place the cursor on the template and select the Insert table action.
  2. In the Resource Type field, select the collection OrderItems you created.
  3. Specify the required number of columns and rows.

  1. Add column labels for Item, Quantity, Unit Price, and Subtotal in the first row.
  2. Place the cursor on the second row and use the , then click 'Make Data Row' to include the values of the collection.

  1. Insert the following into the table from the collection OrderItems using the Insert Field action.

  • Product Name - {!order_items.Product2.Name}
  • Quantity - {!order_items.Quantity}
  • Unit Price - {!order_items.UnitPrice}
  • Total Price - {!order_items.TotalPrice}

You can insert multiple fields into the same cell. Make sure you insert all the collection fields inside the data row.

6 - Insert Condition

Use Insert Condition to show or hide sections based on values from your record.

In the purchase order template, let's display a message only when the Total Order Amount exceeds $10,000

Steps to Insert a Condition:

  1. Place the cursor where you want the conditional content to appear.
  2. Open the CloudFiles Add-in and choose Insert Condition.
  3. In the Field dropdown, select the relevant field (e.g., TotalAmount or Order Total).
  4. Set the Operator (e.g., Greater Than) and enter the Value (e.g., 10000).
  5. Click Insert
  6. Add the message inside <If> to display if the condition is met.

7 - Document Title

Use Document Title action to define the name for your generated document. This title will be automatically applied when the document is generated. You can use values from records and other resources to create dynamic file names. In the template, we have used {!$Record.OrderNumber}to set the document title dynamically based on the order number.

If no Document Title is specified, the document will default to the name of the template.

Global constants

Global constants are predefined values that remain the same for every document. By default, the template includes:

  • $GlobalConstant.EmptyString – blank text value
  • $GlobalConstant.False – boolean false
  • $GlobalConstant.True – boolean true

These constants can be used to add fixed values or control logic.

Stage 2 - Preview

The Preview feature allows you to test how your document will look using real data before publishing it.

How It Works:

  1. Enter a valid Record ID (e.g., a Salesforce Order ID) into the input box.
  2. Click Generate Preview.
  3. CloudFiles will populate the template with actual values from the record, showing you exactly how the final document will appear.

Errors at Preview:

In the Preview section, if the template has any issues, you may encounter errors, such as 'Preview generation failed,' along with a description of the error.

These errors typically indicate problems like malformed queries, etc. Make sure to review the error message and correct the issue accordingly before publishing.

Stage 3 - Publish

Once you've finished building and previewing your template, use the Publish step to save it for use in your Salesforce org.

Publishing Options:

1 - Publish as a New Template

  • Choose this if you're creating a template from scratch.
  • Enter a unique Template Name in the input field.
  • This will create a new version that will be available for use.

2 - Select an Existing Template

  • Choose this option to update or replace a previously published template.
  • Select from the list of available templates in the dropdown.
  • Your current design will overwrite the selected template with the new version.

Once published, the template will be listed under the Document Generation tab β†’DocGen Templates. The Template Id can be used in Flow automation to generate documents using the following flow actions: Generate Document, Generate Document (Sync), Generate Document (Batch).