How to: Filter and Sort a Related List
Use WHERE, ORDER BY, LIMIT, and relationship fields to return only the rows you need.
Use direct SOQL when you need the right records, not just all related records.
This guide shows how to filter by the current record, narrow results, and control row order.
Goal
Return only recent open Cases for the current Account.
<table class="table1">
<thead>
<tr>
<th>Case Number</th>
<th>Subject</th>
<th>Contact</th>
<th>Status</th>
<th>Created</th>
</tr>
</thead>
<tbody><!--{{!
<lineitemsSOQL>
<class>table1</class>
<soql>
SELECT CaseNumber, Subject, Contact.Name, Status, CreatedDate
FROM Case
WHERE AccountId = '{{!Account.Id}}'
AND Status IN ('New', 'Working', 'Escalated')
AND CreatedDate = LAST_90_DAYS
ORDER BY CreatedDate DESC
LIMIT 10
</soql>
<column>CaseNumber</column>
<column>Subject</column>
<column>Contact.Name</column>
<column>Status</column>
<column format-date="MMM d, yyyy">CreatedDate</column>
</lineitemsSOQL>
}}-->
</tbody>
</table>What each clause does
WHERE AccountId = '{{!Account.Id}}'ties the list to the current AccountStatus IN (...)keeps only the statuses you wantCreatedDate = LAST_90_DAYSuses a SOQL date literalORDER BY CreatedDate DESCshows the newest rows firstLIMIT 10keeps the output short and readableContact.Namepulls a parent field from the Case row
Useful filter patterns
Use these patterns often:
When to use relationship fields
Use dot notation when the value lives on a parent record.
Examples:
Contact.NameAccount.Owner.NameOwner.Name
Make sure the same relationship field appears in both SELECT and <column>.
Common fixes
Wrong results: Check the relationship field in the
WHEREclauseQuery error: Wrap text values in single quotes
Blank relationship column: Add that relationship field to
SELECTToo many rows: Add
LIMITand tighten the filter
Keep going
Review Introduction to SOQL for more query patterns
Review Related List Working Examples for grouped and conditional examples
Last updated
Was this helpful?

