Back to Blog

How to Add Conditional Visibility to a Salesforce Document

How to Add Conditional Visibility to a Salesforce Document

Show or hide sections, tables, rows, and clauses in a Salesforce PDF or Word document based on record data. Patterns for Visualforce, Word merge templates, and formula-driven flags, plus the null and empty-header traps.

Rahul Gawale

Nearly every real document has parts that should only appear sometimes. The warranty paragraph only prints for hardware orders. The discount row only appears when a discount exists. The international terms only apply when the billing country is outside the US.

Conditional visibility is the feature that lets one template serve many cases instead of forcing you to clone a template for each variation. This guide covers where to put the logic, how to write it in the common approaches, and the traps that cause blank headers and wrong clauses.

Decide where the decision lives

There are three places a visibility decision can be made, and choosing well saves you a lot of maintenance.

WhereExampleBest for
In the template"Show this block if Country is not US"Simple rules an admin owns
In a formula fieldShow_Warranty__c checkboxRules reused across documents, reports, and flows
In Apex or a FlowA computed flag or filtered listRules that depend on several objects or on counts

A good rule of thumb: if the same rule appears in more than one template, move it to a formula field so there is one definition. If the template holds the only copy, keep it there.

Pattern 1: Visualforce rendered

Every Visualforce component supports a rendered attribute that takes a Boolean expression:

<apex:outputPanel rendered="{!quote.Discount > 0}">
  <tr>
    <td>Discount</td>
    <td>{!quote.Discount}</td>
  </tr>
</apex:outputPanel>

Combine conditions with && and ||, and negate with !:

<apex:outputPanel rendered="{!AND(order.Type == 'Hardware', NOT(ISBLANK(order.Warranty_Terms__c)))}">
  ...
</apex:outputPanel>

rendered="false" removes the markup from the output entirely, so no empty box or spacing is left behind.

Pattern 2: Word merge template tags

Word-based merge tools typically provide an IF and END-IF pair that wraps the content you want to toggle. The exact syntax differs between products, but the structure is the same:

{IF Country != 'US'}
  International shipping terms apply...
{END IF}

Three rules prevent most breakage:

  1. Wrap whole paragraphs or whole table rows. Wrapping half a sentence leaves stray punctuation and spacing when the content is removed.
  2. Keep the opening and closing tag in the same container. A tag opened in one table cell and closed in another can corrupt the document.
  3. Type tags as plain text. Word often splits a tag across several formatting runs, and the engine then fails to recognize it.

Pattern 3: Pre-computed flags

For anything beyond a single field comparison, compute a flag before generation and test only the flag in the template.

A formula field handles most cases:

AND(
  TEXT(Type) = "Hardware",
  Total_Amount__c > 10000,
  NOT(ISBLANK(Warranty_Terms__c))
)

The template then just checks Show_Extended_Warranty__c. The rule is now readable in Setup, testable in a report, and reusable everywhere. When the legal team changes the threshold from 10,000 to 25,000, you update one formula, not five templates.

Hiding rows, tables, and headers

Visibility applies at three levels, and each has its own gotcha.

Row level: hide a line item. Test a flag on the child record, such as Internal_Only__c. Best practice is to filter these records out in the query (WHERE Internal_Only__c = false) so they never reach the template. That also keeps your totals consistent, since a hidden row that still counts toward a subtotal is a common and embarrassing bug.

Table level: hide the whole table when it has no rows. The classic failure is a header that prints above nothing. Test the count of child records, not the existence of the relationship:

<apex:outputPanel rendered="{!lineItems.size > 0}">
  <table>...</table>
</apex:outputPanel>

Column level: show a column only for some products. This is hard in most tools because columns are shared by every row. The usual solution is two table variants, each wrapped in its own condition, rather than a single table with conditional cells.

The traps to avoid

Null is not false. A checkbox is never null, but a text, number, or lookup field can be. Field = 'X' on a null value is false in Salesforce formulas, but a negated test (Field != 'X') can behave differently depending on the engine. Be explicit with ISBLANK when a missing value should have a defined outcome.

Zero and empty are different. A discount of 0 is a value. If you want to hide a discount row at zero, test > 0, not ISBLANK.

Picklist labels versus API values. Test against the value your engine returns. In formulas, use TEXT(PicklistField) and compare against the API value, which does not change when an admin relabels it.

Case and whitespace. Text comparison in templates can be case-sensitive. Normalize with UPPER() or TRIM() in a formula field when the source data is messy.

Spacing left behind. Removing a paragraph can leave a blank line or a double page break. Put the spacing inside the conditional block, not between blocks.

Rules nobody can find. In a Word-based template, logic sits inside the document, hidden from anyone reading it normally. Keep a short list of every rule and its owner, or push rules into formula fields where they appear in Setup.

A simple testing checklist

Test every conditional block with at least these records:

  • A record where the condition is true.
  • A record where it is false.
  • A record where the tested field is blank.
  • A record with zero child rows, to check for orphaned headers.

If you can create these four records in a sandbox, you will catch almost every visibility bug before a customer does.

Frequently asked questions

Can I show or hide content without code? Yes. Formula fields and the template's own condition tags cover most cases without Apex. Code is only needed for logic that spans several objects or needs aggregation.

How do I hide a section when a related list is empty? Test the collection size (for example lineItems.size > 0) and wrap the entire section, heading included, in that condition.

Should the logic live in the template or on the object? If more than one document uses the rule, put it on the object as a formula field. Keep it in the template only when it is truly specific to that one layout.

Why does my conditional block leave a blank page? A page break or a trailing empty paragraph is sitting outside the conditional block. Move it inside, so it disappears with the content.

Summary

Conditional visibility works best when each rule has one home, usually a formula field, and when the condition wraps whole blocks rather than fragments. Handle blanks and zeroes on purpose, filter hidden rows out of the data so totals stay honest, and test the true, false, blank, and empty cases.

To go further, see how to hide line items in a Salesforce quote PDF.

Ready to try ZeroExport?

Start generating documents directly in your Salesforce org. No integrations, no setup overhead, no complexity.