Introduction

This article will teach you how to build an S-Docs template that will generate an entire component template within it for each record in a list of Salesforce records.

Let's say you wanted to create a single summary document of each opportunity related to an account, but you don't want to use the standard related list table formatting. You could remove the table CSS styling by using <class>none</class>, but this will only output your data as a comma-delimited list.

Using this feature, you could create a component template for your related opportunity data and format the data in any way you'd like. Then, when you generate a parent template that references the component template, you'll have a single document with your custom-formatted component template repeating itself for each opportunity record in your account's related list.

This feature works with any list of Salesforce records.

If you need to generate a separate document for each record in a list of records, click here.

This feature involves two templates:

  1. The parent template: This template will end up being the final document where all of the record data is displayed. It is the template that the end user will select and generate.
  2. The component template: S-Docs component templates are templates that are built to be displayed within other templates, and are not generated as stand-alone documents. This template will be used to configure how data from each record is displayed in the parent template. It won't be seen or selected by the end user. Although it's only one template, it will repeat itself in the parent template for each record in the list that you specify.

Create The Parent Template

To begin, we'll create the parent template. Let's say we want to generate a document from the Account object that has data merged in from each opportunity record in the Opportunity related list. Create a new S-Docs template, and ensure that the Related To Type field is set to Account (or whichever base object you're working with).

Navigate to the template editor and click on the Source button. Then, paste something like the following into the source of the template editor.

This LineItemsSOQL statement will be where our component template appears for each record in the related list. This parent template will run one SOQL query for each component template that you want to include (in this example, we're only including one component template). Let's dig a little deeper into what's going on here.

After opening the LineItemsSOQL statement, we've specified the name of our component template, Component_Template, within the <component> tags.

After this, we've written a SOQL query that will allow the component template to pull field data from opportunities related to our account. This feature isn't limited to related lists, however - you can query any object in your org with any filter conditions.

Create The Component Template

Now that we've written our LineItemsSOQL statement in the parent template, it's time to create the component template that will appear there when the parent template is generated.

Create a new S-Docs template, and ensure that the Related To Type field is set to the object that your SOQL query is pulling data from (in this case, Opportunity). Change the Template Format field to Component. This will automatically uncheck the Available For Use and Initially Visible checkboxes when you save the template, ensuring that users won't be able to select and generate this template on its own.

Navigate to the template editor. All you have to do now is build out how you want the data from each record to appear in the parent template, using merge fields in the same way as you always would. We'll make a simple list of data that looks something like this:

Once you click Save, you're ready to generate your document!

Note: You can also reference the merge field {{!RowNum}} in your component template. This will correspond to the row number of the record returned by the LineItemsSOQL statement in the parent template.
Note 2: If you're using Named Queries in your parent template, you can reference these merge fields in your component template as well.
Note 3: Component templates support <nullprefix>, <nullpostfix>, <allprefix>, and <allpostfix> tags.

Generate Your Document

It's time to put our work into action. Navigate to a base object record (in this case, we'll be navigating to an Account record) and click the S-Docs button. Choose your parent template and click Next Step.

Once your document has generated, click the PDF icon to view it. The component template will have generated for each record in your list. Since our account had two opportunities attached to it, the component template generated twice in our parent template.

Although this is only a very simple example, you can use the full power of the S-Docs template editor to style both the component and parent template in any way that you want.