# How To: Chain Named Queries with queryname2

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>
  <class>none</class>
  <queryname>LatestOpp</queryname>
  <soql>
    SELECT Id, Name
    FROM Opportunity
    WHERE AccountId = '{{!Account.Id}}'
    ORDER BY CreatedDate DESC
    LIMIT 1
  </soql>
</lineitemsSOQL>
}}-->

<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <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](/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)](/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](/sdocs/advanced-template-logic/named-query.md) for the broader named query path


---

# 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/named-query/guide-for-using-queryname2.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.
