Generate a Component Template for Each Record in a List of Records

Introduction

This article will teach you how to build an S-Docs template that will generate a 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.

Instead, you can use 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.

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 templateS-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. There are two different ways to write the component query.

Method 1

This method will run 1 SOQL query for each component merged. It's ideal for shorter lists of records. Paste something like the following into the source of the template editor.

<!--{{!<LineItemsSOQL>
<component>Component_Template</component>
<soql>SELECT Id FROM Opportunity WHERE AccountId='{{!Account.Id}}'</soql>
</LineItemsSOQL>}}-->

This LineItemsSOQL statement will be where our component template appears for each record in the related list. 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.

Because this method runs a SOQL query for each component merged, it may not be scalable for large lists of records. In that case, you can use method 2.

Method 2

Note: Method 2 is available in S-Docs 4.447+

This method will run 1 SOQL query in total, no matter how many components are merged. It's ideal for longer lists of records. Paste something like the following into the source of the template editor:

<!--{{!<lineitemsSOQL>
  <class>none</class>
  <soql>SELECT id, name, stagename from opportunity WHERE AccountId='{{!Account.Id}}'</soql>
  <component columnFields="true" mergeFieldPrefix="prefix">Component_Template</component>
  <column componentMergeField="true">id</column>
  <column componentMergeField="true">name</column>
  <column componentMergeField="true">stagename</column>
</lineitemsSOQL>}}-->

This method requires adding the columnFields="true" attribute to your <component> tag to specify that your component template uses the fields that you define below in <column> tags. You must also define a merge field prefix within your <component> tags by using the mergeFieldPrefix="prefix" attribute. This prefix must be used in the merge fields in your component template. Each merge field used in your component must also be defined in a <column> tag with the componentMergeField="true" attribute in the parent template component query.

Refer to the section below to see a syntax example of how your merge fields should look in your component template when using method 2.

Note: You can separate merged components with page breaks by adding the delimiter attribute to your component tag.

Example:

<component delimiter="pagebreak">Component Name</component>

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.

For Method 1, you can simply use merge fields like you always would. We'll make a simple list of data that looks something like this:

Opportunity ID: {{!Opportunity.id}}
Opportunity Name: {{!Opportunity.name}}
Opportunity StageName: {{!Opportunity.stagename}}

For method 2, be sure to use the merge field prefix specified in the parent template, and only reference fields defined in the parent template component query <column> tags.

Opportunity ID: {{!prefix.id}} 
Opportunity Name: {{!prefix.name}} 
Opportunity StageName: {{!prefix.stagename}}

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

Note: For method 1, 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.
For method 2, add a column in your parent template component query like <column componentMergeField="true">rownum</column> and then use {{!MergeFieldPrefix.rownum}} (Replacing MergeFieldPrefix with your chosen prefix)
Note 2: 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.

Tags: , , , ,

Was this helpful?