> 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/sdocs/advanced-template-logic/named-query/guide-for-using-queryname2.md).

# How To: Chain Named Queries with queryname2

Use queryname2 when a second named query depends on a value returned by a first named query.

Use `queryname2` when one named query depends on values returned by another named query.

This pattern lets you run a first query, save its result, and reuse that result inside a second query.

### When to use queryname2

Use `queryname2` when:

* the second query needs a value returned by the first query
* you want to avoid hard-coded values in the second query
* one query result narrows the target record set for the next query

### How queryname2 works

1. Create the first named query with `<queryname>`.
2. Place the dependent query later in the template with `<queryname2>`.
3. Reference the first query's returned fields inside the second query's `<soql>` block.
4. Output values from both queries with normal merge fields.

The first query must appear before the `queryname2` block.

### Example

{% code title="queryname2-example.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <queryname>LatestOpp</queryname>
  <soql>
    SELECT Id, Name
    FROM Opportunity
    WHERE AccountId = '{{!Account.Id}}'
    ORDER BY CreatedDate DESC
    LIMIT 1
  </soql>
</lineitemsSOQL>
}}-->

<!--{{!
<lineitemsSOQL>
  <queryname2>TopLine</queryname2>
  <soql>
    SELECT Id, Quantity, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!LatestOpp.Id}}'
    ORDER BY TotalPrice DESC
    LIMIT 1
  </soql>
</lineitemsSOQL>
}}-->
```

{% endcode %}

{% code title="queryname2-output.txt" %}

```plaintext
Latest opportunity: {{!LatestOpp.Name}}
Top line quantity: {{!TopLine.Quantity}}
Top line total: ${{!TopLine.TotalPrice #,###.00}}
```

{% endcode %}

In this pattern:

* `LatestOpp` stores the first query result
* `TopLine` uses `{{!LatestOpp.Id}}` inside its SOQL
* both results are available later in the template

### Rules to remember

* Put the first named query before the `queryname2` block
* Return every field you plan to reuse later in `SELECT`
* Use stable keys like `Id` when possible
* Add `ORDER BY` and `LIMIT` when more than one row could match

### Common fixes

* **Second query is blank:** Make sure the first query returns a row
* **Second query fails:** Confirm the referenced field is included in the first query `SELECT`
* **Wrong record returned:** Use a more specific filter, `ORDER BY`, or `LIMIT`
* **Microsoft template output prints as text:** Wrap output fields in square brackets

{% hint style="info" %}
Build and test the first named query by itself first.

Then add the `queryname2` block once the first result is correct.
{% endhint %}

### Keep going

* Review [How to: Advanced Data Retrieval with Named Queries](https://help.sdocs.com/sdocs/advanced-template-logic/named-query/how-to-advanced-data-retrieval-with-named-queries.md) for filters, offsets, and aggregates
* Review [Named Queries for PDF Upload Templates (Reference)](https://help.sdocs.com/sdocs/template-architecture/document-formats/pdf-upload-format/named-queries-for-pdf-upload-templates-reference.md) if you need named queries in PDF Upload templates
* Return to [Retrieve and Reuse Data](https://help.sdocs.com/sdocs/advanced-template-logic/named-query.md) for the broader named query path


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/named-query/guide-for-using-queryname2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
