Products
Document Generation
Generate any doc from Salesforce in 1 click
Template Editor
Conditional Logic
Batch Processing
Native E-Signature
Dochly Storage
Connect cloud storage to any Salesforce record automatically
Industries
🏥
Healthcare
HIPAA native
🏦
Financial Services
🏛️
Government
💻
Technology
🏭
Manufacturing
View all 9 industries →
Departments
📈
Sales
Close deals faster
⚙️
Business Operations
💬
Customer Service
👥
Human Resources
📍
Field Service
View all 8 departments →
Resources
Blog
Case Studies
About Dochly
Help Centre
Contact Us
Dochly Storage Pricing Start Free Trial
How to Add Conditional Logic to Salesforce Documents (IF/THEN) (2026)
How to add conditional logic to Salesforce documents: IF/THEN rules to show and hide sections based on field values
How-To Guide Conditional Logic Document Automation Dynamic Content

How to Add Conditional Logic to Salesforce Documents (IF/THEN) (2026)

Salesforce conditional document logic is the ability to show or hide sections, clauses, and content in a document template based on the value of a Salesforce field. Instead of maintaining separate templates for every document variant — one for enterprise clients, one for SMBs, one for UK customers, one for US customers — a single template handles all variants through IF/THEN rules that activate the right content for each record automatically.

Without salesforce conditional document logic, teams either over-include content — everything appears in every document regardless of relevance — or maintain a sprawling library of nearly identical templates that break every time the underlying content needs updating. Salesforce conditional document logic solves both problems with one mechanism.

This guide covers exactly how to add conditional logic to Salesforce document templates: the syntax, the use cases, compound conditions, nested logic, and how to test that your conditions fire correctly. For the foundational template setup, see our guide on how to create Salesforce document templates.

What is conditional logic in Salesforce document generation?

Salesforce conditional document logic is a rule-based system in a document template that evaluates a Salesforce field value at generation time and includes or excludes content based on whether the condition is met. Every conditional section in a template is governed by an IF/THEN rule that references a specific field on the Salesforce record being generated.

The rule follows an IF/THEN structure: IF a specified field equals (or does not equal, exceeds, or contains) a specified value, THEN include the associated content block in the generated document.

When a document is generated from a Salesforce record, the template engine evaluates every conditional block in the template against that record’s field values. Blocks whose conditions are met are included in the output. Blocks whose conditions are not met are removed entirely. The surrounding content reflows to fill the space, and the final document contains only the content appropriate for that specific record.

The result is a single template that produces correctly tailored output for every record it is run against — without any manual editing after generation.

When should you use conditional sections in Salesforce documents?

Use salesforce conditional document logic whenever a document needs to vary its content based on who it is for, what the deal looks like, or where the record originates. The goal is always the same: one template that applies salesforce conditional document logic to produce the correct output for every record automatically.

Enterprise vs. standard terms

Show enterprise SLA clauses, dedicated account manager assignments, and custom payment terms only when the Opportunity record type is Enterprise. Standard customers receive the baseline terms without the enterprise-specific content appearing.

Jurisdiction-specific legal clauses

Show UK governing law when the Account billing country is United Kingdom. Show GDPR data processing clauses when the contact’s country is in the EU. Show CCPA language when the billing state is California. All in the same template.

Compliance and regulatory sections

Include HIPAA business associate agreement sections only for accounts in the Healthcare industry. Include SOX audit requirements only for publicly listed companies. Keep the template clean for accounts where those sections are irrelevant.

Volume and value-based clauses

Include a volume discount schedule only when the Opportunity amount exceeds a threshold. Add an extended warranty section only when a specific product line is included in the line items. Trigger a relocation budget clause only for candidates outside the hiring city.

Signature block variations

Show a two-party signature block for standard agreements, a three-party block when a guarantor field is populated, or a board authorisation section when the deal value requires additional approval signatories.

Expiry and urgency language

Include a “This offer expires within 14 days” clause only when a custom boolean field is checked on the Opportunity. Remove it from standard agreements where the expiry language is not appropriate.

Salesforce conditional document logic use cases: enterprise terms, jurisdiction clauses, compliance sections, value-based content

Salesforce conditional document logic applies across contract terms, legal clauses, compliance sections, and signature blocks — all from a single template.

Read more: How to Generate Documents in Salesforce: Step-by-Step Guide (2026)

How IF/THEN logic works in Salesforce document templates

Salesforce conditional document logic in a template works by wrapping content in an IF block that references a Salesforce field API name, a comparison operator, and a trigger value. The result is a template that applies different content rules to different records without any manual intervention. The template engine evaluates the condition at generation time and includes or excludes the wrapped content based on the result.

The exact syntax varies by document generation tool, but the logical structure is consistent across all implementations. Most tools use a tag-based or comment-based approach to mark the start and end of conditional blocks directly inside the Word template or native editor.

Basic IF/THEN syntax {{#if Opportunity.RecordType.Name == "Enterprise"}} This agreement is subject to enterprise service level terms. A dedicated account manager will be assigned within five business days of contract execution. {{/if}}
IF/THEN with ELSE clause {{#if Contact.MailingCountry == "United Kingdom"}} This agreement is governed by the laws of England and Wales. {{else}} This agreement is governed by the laws of the State of New York. {{/if}}
Numeric comparison operators {{#if Opportunity.Amount > 50000}} Volume pricing applies. See Schedule B for the applicable tier rates. {{/if}}   {{#if Account.NumberOfEmployees >= 1000}} Enterprise support terms apply. See Exhibit C. {{/if}}
Boolean field conditions {{#if Opportunity.Include_NDA__c == true}} NON-DISCLOSURE AGREEMENT The parties agree that all information shared under this agreement... {{/if}}

The field reference inside the IF block uses the same merge field syntax as the rest of the template — the Salesforce field API name, including the __c suffix for custom fields and __r for relationship traversal. See our Salesforce merge fields guide for the full syntax reference.

How IF/THEN logic works inside Salesforce documents: conditional block evaluating record type field at generation time

An IF block in a Word template wraps the enterprise clause. When the document is generated from an Enterprise record, the clause appears. From a Standard record, it is removed entirely.

How to add conditional logic to a Salesforce document template: step by step

Adding salesforce conditional document logic to a template follows five steps: identify conditional sections, choose the controlling field, write the IF rule, add an ELSE clause if needed, and test against both sides of the condition. Each step is covered in detail below.

1

Identify the sections that should be conditional

Review your document template and mark every section, clause, table, or piece of content that should appear only for certain records. Group related conditions together — all jurisdiction-based clauses in one area, all deal-value clauses in another — so the template remains readable after conditional blocks are added.

2

Choose the controlling Salesforce field

Select the Salesforce field whose value will determine whether each conditional section appears. This can be any field on the primary record or a related record — record type, picklist value, boolean, currency amount, or text field. Find the field’s API name in Setup under Object Manager — Fields and Relationships.

3

Write the IF/THEN rule in the template

Wrap the conditional content in an IF block using your document generation tool’s syntax. Place the opening IF tag immediately before the first word of the conditional content and the closing tag immediately after the last word or punctuation mark. Avoid placing the tags on their own line if possible — this prevents unwanted blank lines from appearing when the condition evaluates to false.

4

Add an ELSE clause for alternative content

For sections where alternative content should appear when the condition is not met, add an ELSE clause inside the same IF block. The ELSE clause is optional — omit it if there is simply no content for the false case. The section will disappear cleanly when the condition is false without producing blank space.

5

Test against records on both sides of the condition

Generate test documents from at least one record where the condition is true and one where it is false. Verify the correct content appears in each output and that no blank lines, rogue characters, or formatting artefacts remain where the hidden content was removed. See the testing section below for a complete checklist.

Compound conditions: AND and OR logic in Salesforce document templates

Salesforce conditional document logic supports compound conditions that combine multiple field checks using AND and OR operators. Compound conditions let a single salesforce conditional document logic rule evaluate two or more fields simultaneously before deciding whether to include or exclude a section. AND conditions require all clauses to be true for the section to appear. OR conditions require at least one clause to be true.

AND condition — both must be true {{#if Opportunity.RecordType.Name == "Enterprise" AND Opportunity.Amount > 100000}} EXECUTIVE SUPPORT TERMS Accounts qualifying for Executive Support receive a dedicated VP of Customer Success... {{/if}}
OR condition — either must be true {{#if Account.Industry == "Healthcare" OR Account.Industry == "Financial Services"}} REGULATORY COMPLIANCE OBLIGATIONS The parties acknowledge that this agreement is subject to applicable regulatory requirements including but not limited to HIPAA and SOX where applicable. {{/if}}

Combine AND and OR with parentheses to build more complex conditions. Most document generation tools support parenthetical grouping that mirrors standard Boolean logic. Test compound conditions thoroughly — each additional operator multiplies the number of record combinations that need verification.

When combining AND and OR in the same condition, use parentheses to make the intended logic explicit. Without parentheses, different tools may evaluate operator precedence differently. Explicit grouping eliminates ambiguity: (A AND B) OR C is unambiguous; A AND B OR C is not.

Nested conditional sections in Salesforce document templates

Nested salesforce conditional document logic places one IF block inside another, allowing a section to be conditional on a combination of criteria evaluated in sequence. An outer condition might qualify the broad category, while inner conditions handle specific variants within it.

Nested IF blocks — outer and inner conditions {{#if Opportunity.RecordType.Name == "Enterprise"}} This agreement is subject to enterprise service level terms.   {{#if Account.BillingCountry == "United Kingdom"}} This agreement is additionally governed by the laws of England and Wales. {{/if}}   {{#if Opportunity.Amount > 250000}} Executive Business Review cadence applies at this contract value. {{/if}} {{/if}}

In this example, the enterprise service level section only appears for Enterprise records. Within that section, the UK jurisdiction clause appears only for Enterprise records in the UK, and the Executive Business Review clause only for Enterprise records above £250,000. A standard record or a non-UK enterprise record sees only the relevant content.

Keep nesting to two levels maximum. Deeply nested conditional logic is difficult to maintain and prone to unclosed tag errors. If a section requires three or more levels of nesting, consider whether compound AND conditions or separate template variants would be cleaner for the specific use case.

Nested conditional logic in a Salesforce document template: outer Enterprise condition with inner UK jurisdiction and deal value conditions

Nested IF blocks handle hierarchical document logic. The outer condition qualifies the record type; inner conditions handle specific variants within that category.

Testing conditional logic in Salesforce document templates

Salesforce conditional document logic must be tested systematically before a template goes into production. Unlike merge field errors that produce blank output, a conditional logic error either includes content that should not be there or excludes content that should — both produce serious problems in contracts and compliance documents.

For every condition in the template, test a minimum of two records: one where the condition evaluates to true and one where it evaluates to false. For compound conditions, test records that satisfy each clause independently and records that satisfy both.

Conditional logic testing checklist: For each IF block — verify the section appears when the condition is true. Verify the section is absent when the condition is false. Confirm no blank lines or extra whitespace remain when content is removed. Verify the ELSE clause appears when the condition is false and is absent when it is true. For nested blocks — test every valid combination of outer and inner conditions.

Frequently asked questions about Salesforce conditional document logic

What is conditional logic in Salesforce document generation?
Conditional logic in Salesforce document generation is the ability to show or hide sections, clauses, and content in a document template based on the value of a Salesforce field on the record being generated. An IF/THEN rule checks whether a field meets a condition, and if it does, includes the relevant content in the output document.
Can I use conditional logic with custom Salesforce fields?
Yes, salesforce conditional document logic can reference any Salesforce field that is accessible to the running user, including custom fields with the __c suffix. You can also traverse relationships to condition sections on related record field values, such as the Account industry or the Opportunity record type.
Can I nest conditional sections inside each other?
Yes, most native Salesforce document generation tools support nested conditional logic. An outer IF block can contain inner IF blocks that apply additional conditions to subsets of content. Keep nesting to two levels maximum — deeper nesting is difficult to maintain and prone to unclosed tag errors.
Do conditional sections affect document pagination?
Yes. When a conditional section is hidden because its condition is not met, the content is removed and the surrounding text reflows to fill the space. Page breaks, numbering, and section spacing all adjust dynamically. For documents with precise pagination requirements, test with and without each conditional section to confirm the layout remains correct in both states.
Can conditional logic reference multiple fields at once?
Yes. Most document generation tools support compound conditions using AND and OR operators. A section can be shown only when the Opportunity record type equals Enterprise AND the deal value exceeds 50000, or when either the industry equals Healthcare OR the industry equals Financial Services.

Salesforce conditional document logic is one of the highest-value capabilities in document generation. A single template that produces correctly tailored output for every record type, region, deal size, and industry eliminates the template sprawl and manual editing that holds most teams back from true document automation.

For the foundation that makes conditional logic possible, see our guide on how to create Salesforce document templates, or visit Dochly document generation to see conditional logic in action inside a fully native Salesforce app.

Umer Balaj
11x Salesforce Certified Developer and Architect
Umer Balaj is an 11x Salesforce Certified Developer and Architect with 11,000+ hours of Salesforce delivery on Upwork (Top Rated Plus, 100% Job Success). He built Dochly as a 100% native Salesforce document generation and e-signature app. Umer specialises in Apex, LWC, Flows, and complex integrations across Health Cloud, Financial Services Cloud, Sales Cloud, and Service Cloud.