> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/developer-hub/template-configuration-and-styling/related-list-references/related-list-working-examples.md).

# Related List Working Examples

Each example below uses the standard related list structure:

* `<!--{{!` and `}}-->`
* `<lineitemsSOQL>`
* `<class>`
* `<soql>`
* `<column>`

{% stepper %}
{% step %}

#### Aggregate Query: Summing Totals by Account

This groups Opportunities by Account and sums the related amounts.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Account</th>
      <th>Total Amount</th>
      <th>Opportunity Count</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Account.Name, SUM(Amount) total, COUNT(Id) qty
    FROM Opportunity
    WHERE AccountId != null
    GROUP BY Account.Name
    ORDER BY SUM(Amount) DESC
    LIMIT 10
  </soql>
  <column>Account.Name</column>
  <column format-number="$#,###.00">total</column>
  <column postfix=" deals">qty</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Three-Level Parent Traversal

This traverses three parent levels: Case → Contact → Account → Owner.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Case Number</th>
      <th>Contact</th>
      <th>Account</th>
      <th>Account Owner</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT CaseNumber, Contact.Name, Contact.Account.Name, Contact.Account.Owner.Name
    FROM Case
    WHERE Status != 'Closed'
    LIMIT 15
  </soql>
  <column>CaseNumber</column>
  <column>Contact.Name</column>
  <column>Contact.Account.Name</column>
  <column>Contact.Account.Owner.Name</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Date Literals & Status Badging

This uses a SOQL date literal and the `render` attribute for status output.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Opportunity</th>
      <th>Stage</th>
      <th>Close Date</th>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Name, StageName, CloseDate, Amount
    FROM Opportunity
    WHERE CloseDate = LAST_90_DAYS AND Amount > 0
  </soql>
  <column>Name</column>
  <column type="rtf" render="RECORD.StageName == 'Closed Won', lt#span style='color:green; font-weight:bold;'gt#WONlt#/spangt#, RECORD.StageName == 'Closed Lost', lt#span style='color:red; font-weight:bold;'gt#LOSTlt#/spangt#, RECORD.StageName"></column>
  <column format-date="MMM d, yyyy">CloseDate</column>
  <column format-number="$#,###.00">Amount</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Polymorphic Fields (Owner)

This pulls task owner data and highlights high-priority tasks.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Priority Flag</th>
      <th>Subject</th>
      <th>Owner</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Subject, Priority, Owner.Name
    FROM Task
    WHERE Status != 'Completed'
    ORDER BY Priority DESC
  </soql>
  <column render="RECORD.Priority == 'High', URGENT, RECORD.Priority"></column>
  <column>Subject</column>
  <column>Owner.Name</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Complex Filtering with OR/AND Logic

This uses grouped `OR` and `AND` conditions in the `WHERE` clause.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Account</th>
      <th>Industry</th>
      <th>Annual Revenue</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Name, Industry, AnnualRevenue
    FROM Account
    WHERE (BillingState = 'CA' OR BillingState = 'NY')
      AND (Type = 'Customer - Direct' OR Type = 'Customer - Channel')
      AND AnnualRevenue > 1000000
  </soql>
  <column>Name</column>
  <column>Industry</column>
  <column format-number="$#,###">AnnualRevenue</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Merging & Cleaning Contact Info

This combines `mergenext`, `substitute`, `prefix`, and `postfix`.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Title</th>
      <th>Department</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>SELECT FirstName, LastName, Title, Department FROM Contact</soql>
  <column mergenext="true" postfix=" ">FirstName</column>
  <column>LastName</column>
  <column substitute="VP, Vice President, Dir, Director">Title</column>
  <column prefix="(" postfix=")">Department</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### "Top N" Records with Null Handling

This returns the top 5 Opportunities by amount and handles null descriptions.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Opportunity</th>
      <th>Amount</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Name, Amount, Description
    FROM Opportunity
    WHERE Amount != null
    ORDER BY Amount DESC
    LIMIT 5
  </soql>
  <column>Name</column>
  <column format-number="$#,###">Amount</column>
  <column nullprefix="Standard Deal">Description</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Conditional Visibility based on Parent Field

This uses a parent merge field in `showcolumn`.

This example assumes the template runs from an Opportunity record.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Product</th>
      <th>Quantity</th>
      <!--RENDER='{{!Opportunity.Account.Rating}}' != 'Hot' --><th>Discount</th><!--ENDRENDER-->
      <th>Total Price</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Name, Quantity, Discount__c, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
  </soql>
  <column>Name</column>
  <column>Quantity</column>
  <column showcolumn="{{!Opportunity.Account.Rating}} != 'Hot'">Discount__c</column>
  <column format-number="$#,###.00">TotalPrice</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Long Text Handling in Descriptions

This strips markup and forces line breaks inside long text values.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Product</th>
      <th>Product Code</th>
      <th>Custom URL</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>
    SELECT Name, ProductCode, CustomURL
    FROM Product2
    WHERE IsActive = true
  </soql>
  <column>Name</column>
  <column>ProductCode</column>
  <column type="text" breakeverynchars="30">CustomURL</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}

{% step %}

#### Replacing Partial Text Strings

This uses `replaceall` to swap internal codes with user-facing text.

```xml
<table class="table1">
  <thead>
    <tr>
      <th>Case</th>
      <th>Comments</th>
    </tr>
  </thead>
  <tbody><!--{{!
<lineitemsSOQL>
  <class>table1</class>
  <soql>SELECT Name, Comments__c FROM Case</soql>
  <column>Name</column>
  <column replaceall="ERR-001, System Error, WRN-002, Warning">Comments__c</column>
</lineitemsSOQL>
}}-->
  </tbody>
</table>
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.sdocs.com/developer-hub/template-configuration-and-styling/related-list-references/related-list-working-examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
