For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

filtered-related-list.xml
<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 Account

  • Status IN (...) keeps only the statuses you want

  • CreatedDate = LAST_90_DAYS uses a SOQL date literal

  • ORDER BY CreatedDate DESC shows the newest rows first

  • LIMIT 10 keeps the output short and readable

  • Contact.Name pulls 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.Name

  • Account.Owner.Name

  • Owner.Name

Make sure the same relationship field appears in both SELECT and <column>.

Common fixes

  • Wrong results: Check the relationship field in the WHERE clause

  • Query error: Wrap text values in single quotes

  • Blank relationship column: Add that relationship field to SELECT

  • Too many rows: Add LIMIT and tighten the filter

Keep your first query simple. Then add one filter at a time.

Keep going

Last updated

Was this helpful?