If you want to merge a section on your generated document based on a condition of a checkbox or formula field, you can do this using the section merge feature in the template builder. You can also create a conditional page break in your template as well.

Conditional Merge Based on a Checkbox Field

For example, you could base the section merge from a standard checkbox field (true or false value) on your Opportunity object to include or not include a “services agreement.” You could name this checkbox field “Send Services Agreement” and if on the Opportunity record, this checkbox field was checked (true), then this additional agreement section would merge onto the template. If this checkbox field was not checked (false), then this agreement section would NOT merge onto the document.

There will be a start and end to each conditional merge section that you will create using the embedded template builder. Your conditional merge section start and end tags should be on their own line in the template.

After the merging of start/end conditional section is completed within generated document, these tags and lines will be removed from the document so you will not have that extra space in document.

Note that if you have a conditional merge section end tag at the end of the document template, you must add one extra line below that tag so the conditional end section end tag is not the last line of the entire document.

See following steps to setup your conditional merge sections:

  1. Select the related checkbox field from in embedded template builder. For this example, we are using “Send Services Agreement.”
  2. Once you type your section name, then click on “Start of Section” to the right next to your section name and it will modify the merge field code to: {!Opportunity.Send_Services_Agreement__c[SECTION_START:Services Agreement]}Copy this merge field code and paste it where you want the section to start on your template.
  3. Now go back to Section and select from drop down “End of Section” and copy the updated merge field code:{!Opportunity.Send_Services_Agreement__c[SECTION_END:Services Agreement]}Copy this merge field code and paste it where you want the section to end on your template. 
    • If you are placing the Section End merge field at bottom of your template, this may cause “Read Time Out ” error with Google script and you can add an empty new line below the merge field to resolve so the merge field is not the last line in template.
  4. Then type in any information you would to show on document (for our example this would be the verbiage from the “Services Agreement”) or you could place a merge field inside of section that you would want to display if value is true.

Conditional Merge Based on a Formula Field

If you would like to merge conditionally without using a checkbox field, then you can create a formula field that is checkbox type and build that formula logic based on any field available to result in a true or false value.

To use any logic you want, including a picklist, you can create a formula field that results in a checkbox as in true/false and put your if statements in the formula looking at the picklist field. such as If(ispickval, field, “value”,true, false).

For example, you could create a conditional merge section based on a formula field on an Opportunity record to merge ONLY if there is at least one product related to the Opportunity.

  1. Create the section and content on template builder.
  2. Create a formula field with the type as checkbox on the object merging from such as Opportunity.  The formula field in our use case could be called “Line Items Exist”.  This formula would have an IF statement to check whether the record has at least one product related to the opportunity, then return true or false.
  3. Go to the template builder and now select the new formula field just created.  Then select to make that a conditional section. 
  4. Copy that merge field code to place the starting of the section at the beginning of the section, and the end at the end of the section.


Multiple Conditional Section Merge

If you would like to have one checkbox field that refers to multiple conditional merge sections in template, you can see this use case article for more detail.

Conditional Page Break

You can create a page break using the conditional section merge feature as follows:

[begin conditional section]
<<add page break here>>
[end conditional section]

Note: Conditional sections are currently not supported in related lists (tables).