Use Conditional Logic with Related Lists in Dochly
How related lists and conditions interact
In a Dochly template, a related list section iterates over child records of the primary object — for example, Opportunity Products on an Opportunity, Contacts on an Account, or Tasks on a Case. Conditional logic interacts with related lists at several levels.
Parent-level conditions control the overall document structure — which sections appear based on Opportunity fields. Child-level conditions (row filters) control which records from each related list appear within those sections. Both levels work independently and can be combined.
Field scope in related list conditions
The fields available in a condition depend on where the condition is applied in the template:
Conditions on a related list section (the container wrapping the entire list) use fields from the parent object. Example: show the Products section only when Opportunity.Has_Products__c is true. The condition evaluates once — before iterating over any child records.
Conditions on the repeating data row use fields from the child object. Example: show a row only when OpportunityLineItem.Quantity > 0. The condition evaluates per child record — once for each product line as Dochly iterates the list.
To use a parent field in a row-level condition, create a formula field on the child object that references the parent — e.g. a formula on OpportunityLineItem that copies Opportunity.Account.Type. This brings the parent value into the child record's field scope, making it available in row conditions.
Four conditional techniques for related lists
Filter which child records appear
Apply a condition to the repeating row — only records that meet the condition are included. Covered in detail in Dynamic tables. This page focuses on the related list-specific patterns.
Separate records into labelled groups
Use multiple related list blocks from the same child object, each with a different row filter — creating grouped sections (Products / Services, Open Tasks / Completed Tasks) from one related list.
Show/hide the whole list section
Apply a show/hide condition to the entire related list section — show it only when the parent object has a relevant field value, or only when the list has at least one matching record.
Conditions based on record count
Use @count variables to show sections, fallback messages, or summary lines based on how many records passed the filter — e.g. "5 active products" or "No open tasks".
Filtering related records
Row-level filtering on related lists works the same as table row filtering — apply a condition to the repeating row block and set it to "Show when condition is true". The condition evaluates against each child record's fields as Dochly iterates.
// Related list iteration with row filter
// {{#each OpportunityLineItems}} iterates over all child records
// The condition on the row block filters which ones render
{{#each OpportunityLineItems}}
{{if Product2.IsActive == true and Quantity > 0}}
{{Product2.Name}} | {{Quantity}} | {{UnitPrice | currency}} | {{TotalPrice | currency}}
{{/if}}
{{/each}}
// Only active products with quantity > 0 are included in the output.
// Inactive products and zero-quantity lines are silently skipped.
The filter condition can use multiple fields from the child record with AND/OR logic. All fields on the child object and its directly related objects (e.g. Product2.Family, Product2.IsActive) are available in the visual condition builder for that row.
Grouping related records into separate sections
To present related records in labelled groups — Products followed by Services, or Open Tasks followed by Completed Tasks — use multiple related list blocks from the same child object, each with a different row filter. Each block renders independently.
// Two related list blocks — same child object, different row filters
// Block 1: Products section
PRODUCTS
{{#each OpportunityLineItems}}
{{if Line_Type__c == "Product"}}
{{Product2.Name}} | {{Quantity}} | {{UnitPrice | currency}}
{{/if}}
{{/each}}
// Block 2: Services section (separate block, same child object)
PROFESSIONAL SERVICES
{{#each OpportunityLineItems}}
{{if Line_Type__c == "Service"}}
{{Product2.Name}} | {{Quantity}} | {{UnitPrice | currency}}
{{/if}}
{{/each}}
Apply an empty-state handler to each grouped section using @rowCount == 0 — and pair it with a section-level show/hide to hide the entire "PROFESSIONAL SERVICES" heading and block when no service lines exist. This keeps the document clean when a deal has only products or only services.
Show or hide the entire related list section
Apply a section-level show/hide condition to control whether the entire related list block — heading, table, and all rows — appears in the document. Section-level conditions use parent object fields.
Select the related list container in the editor
Click the section heading or the outer container that wraps the entire related list block — not an individual row inside it. In the template editor, related list sections are typically wrapped in a named section block. Click the section name or the wrapper element to select the entire container.
Add a condition using a parent object field
With the section container selected, open Conditional Logic → Add Condition. Use a parent object field — e.g. Has_Services__c is true (a checkbox formula on the Opportunity that's true when any service line exists). Set Action to Show when condition is true.
Result: section appears only when relevant
When the condition is false, the entire section — heading, table, all rows — is invisible in the generated document. When true, the section appears with its row-level filters already applied. Parent and row conditions stack — the section only shows when the parent condition passes, and then only matching rows appear within it.
// Section visibility + row filtering combined
// Section condition (parent field): Has_Services__c == true
// If false: the entire SERVICES block is invisible
// If true: block renders, row filter then applies
PROFESSIONAL SERVICES // <-- section hidden if Has_Services__c is false
{{#each OpportunityLineItems}}
{{if Line_Type__c == "Service"}} // row filter still applies within
{{Product2.Name}} | {{UnitPrice | currency}}
{{/if}}
{{/each}}
Count-based conditions
Dochly provides built-in count variables for related list blocks — use them to show different content based on how many records passed the row filter.
// @rowCount — number of rows rendered after filtering
// @totalCount — total number of child records before filtering
// Show a summary line with the count of visible items
{{if @rowCount > 0}}
{{@rowCount}} active product{{if @rowCount == 1}} is{{else}}s are{{/if}} included in this order.
{{else}}
No active products have been added to this order.
{{/if}}
// Show a section only when count exceeds threshold
{{if @rowCount >= 10}}
This order qualifies for volume pricing. Your account manager will be in touch.
{{/if}}
The number of rows rendered after row-level filters are applied. This is the visible row count — use it for empty-state detection, summary sentences ("3 items included"), and count-based section visibility.
The total number of child records before any row filters are applied. Useful for showing how many records existed vs how many were included — e.g. "Showing 3 of 7 tasks" in a project summary that filters to only open tasks.
Pattern library
Show products and services in separate labelled sections
Two OpportunityLineItems blocks: Block 1 row filter Line_Type__c == "Product", Block 2 row filter Line_Type__c == "Service". Each block has its own section heading and empty-state handler. A formula field Has_Services__c on the Opportunity controls section-level visibility for Block 2.
Show only open Opportunities on an account statement
Opportunities block with row filter: IsClosed == false. Excludes Closed Won and Closed Lost deals. Use @rowCount to show "No open opportunities at this time" when all deals are closed.
Group open and completed tasks in separate sections
Two Tasks blocks: Open tasks — filter Status != "Completed"; Completed tasks — filter Status == "Completed". Apply section-level show/hide to the Completed section using a parent formula field Has_Completed_Tasks__c so it only renders when there are completed items.
Show a "volume order" note when more than 10 items are ordered
After the OpportunityLineItems block, add a conditional paragraph: {{if @rowCount >= 10}}This order qualifies for our volume fulfilment programme. Expected dispatch within 5 business days.{{/if}}. The note only appears on large orders, adding context without manual editing.
Show "Showing X of Y tasks" when tasks are filtered
After an Activity block that filters to open tasks only: {{if @rowCount < @totalCount}}Showing {{@rowCount}} open of {{@totalCount}} total activities.{{/if}}. Informs the reader that a filter is active — they know the full history exists but isn't all shown.
Frequently asked questions
Opportunity.Account.Type. This brings the Account.Type value onto the line item, making it available in the row condition. Two levels of formula lookups are reliable; deeper chains may cause formula complexity issues.@rowCount counts rendered rows — rows that passed the row filter. @totalCount counts all records before filtering. If @rowCount is showing the total, check that your row filter condition is correctly configured on the repeating row and that the condition is actually filtering records in your preview. Test using a record where some rows should be filtered out — generate the document and count the actual rows to confirm.Related list conditions now give you complete control over child record content in every document. Final guide in this series: Conditional logic troubleshooting — common errors and fixes in Dochly — diagnose and fix every conditional logic issue including conditions that never fire, wrong field scope, and syntax errors.
Rated 5 stars · Native Salesforce app · Free to install