# Use lineitemsSOQL for multiple filtered related lists

Use `lineitemsSOQL` when a template includes more than one filtered version of the same related list.

Legacy `<lineitems>` blocks can apply one filter to both lists. `lineitemsSOQL` keeps each filter separate.

### Problem

If one template contains multiple legacy related lists with different `<where>` clauses, both lists can render using the same filter.

This usually appears when you split one related list into separate sections, such as:

* hardware products
* service products
* open items and closed items

### Solution

Convert each legacy related list to its own `<lineitemsSOQL>` block.

Each block uses its own SOQL query, so each list keeps its own `WHERE`, `ORDER BY`, and `LIMIT` logic.

### What to change

{% stepper %}
{% step %}

### Change the tag name

Replace `<lineitems>` with `<lineitemsSOQL>`.
{% endstep %}

{% step %}

### Use the object name in the query

Query the Salesforce object in the `FROM` clause.

Use `OpportunityLineItem`, not the relationship name `opportunitylineitems`.

For custom objects, replace `__r` with `__c`.
{% endstep %}

{% step %}

### Link the query to the current record

Add a `WHERE` clause that ties the child object to the parent record.

Example: `WHERE OpportunityId = '{{!Opportunity.Id}}'`
{% endstep %}

{% step %}

### Select every displayed field

Any field you display in a `<column>` should also appear in `SELECT`.
{% endstep %}
{% endstepper %}

### Example conversion

This legacy related list filters Opportunity Products by product family.

{% code title="legacy-lineitems.xml" %}

```xml
<!--{{!
<lineitems>
  <listname>opportunitylineitems</listname>
  <where>PricebookEntry.Product2.Family = 'Hardware'</where>
  <column>PricebookEntry.Product2.Name</column>
  <column>Quantity</column>
  <column prefix="$" format-number="#,###.00">TotalPrice</column>
</lineitems>
}}-->
```

{% endcode %}

Convert it to `lineitemsSOQL` like this:

{% code title="lineitemssoql.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <soql>
    SELECT PricebookEntry.Product2.Name, Quantity, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
      AND PricebookEntry.Product2.Family = 'Hardware'
  </soql>
  <column>PricebookEntry.Product2.Name</column>
  <column>Quantity</column>
  <column prefix="$" format-number="#,###.00">TotalPrice</column>
</lineitemsSOQL>
}}-->
```

{% endcode %}

If you need a second list, create a second `lineitemsSOQL` block with its own `WHERE` clause.

For example, a second block could filter `Family = 'Services'` without affecting the first block.

{% hint style="info" %}
`lineitemsSOQL` is the right choice when one template needs multiple filtered views of the same related list.
{% endhint %}

### Key detail

`opportunitylineitems` is a relationship name. `OpportunityLineItem` is the object name.

That object name belongs in the SOQL `FROM` clause.

### Keep going

* Review [How to: Filter and Sort a Related List](/sdocs/advanced-template-logic/related-lists/how-to-filter-and-sort-a-related-list.md)
* Review [How to: Format and Summarize a Related List](/sdocs/advanced-template-logic/related-lists/how-to-format-and-summarize-a-related-list.md)


---

# Agent Instructions: 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:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/related-lists/additional-troubleshooting/related-lists-display-same-values-converting-lineitems-to-lineitemssoql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
