# Quick Reference: Named Queries for PDF Upload Templates

## Named Queries

Use named queries in PDF Upload templates when you need to place dynamic values on top of a static uploaded PDF.

Standard related lists and DirectSOQL related lists do not work in PDF Upload templates because the uploaded background itself does not merge data.

### Solution 1: Return one field from one record

Use this pattern when you need a single value from the returned record.

#### Named query

{% code title="solution-1-named-query.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery1</queryname>
<soql>SELECT Name, Id FROM Opportunity WHERE opportunity.id='006Da00000NOcmwIAD' LIMIT 1</soql>
</LineItemsSOQL>}}-->
```

{% endcode %}

#### Merge field

{% code title="solution-1-merge-field.txt" %}

```
{{!myQuery1.Id}}
```

{% endcode %}

#### Result

{% code title="solution-1-result.txt" %}

```
006Da00000NOcmwIAD
```

{% endcode %}

### Solution 2: Return multiple fields from the same query

Use this pattern when one named query returns several values you want to place in different PDF Upload fields.

#### Named query

{% code title="solution-2-named-query.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery2</queryname>
<soql>SELECT quantity, listprice, totalprice, productcode FROM opportunitylineitem WHERE opportunity.id= '006Da00000NOcmwIAD'</soql>
</LineItemsSOQL>}}-->
```

{% endcode %}

#### Merge fields

{% code title="solution-2-merge-fields.txt" %}

```
{{!myQuery2.quantity}}
{{!myQuery2.listprice}}
{{!myQuery2.totalprice}}
```

{% endcode %}

#### Result

{% code title="solution-2-result.txt" %}

```
1.00
2.00
2.00
```

{% endcode %}

### Solution 3: Use a filter and offset to target a later row

Use this pattern when your named query returns several rows and you need a specific row from a filtered result set.

#### Named query

{% code title="solution-3-named-query.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery2</queryname>
<filter id="1">TotalPrice != 0</filter>
<soql>SELECT quantity, listprice, totalprice, productcode FROM opportunitylineitem WHERE opportunity.id= '006Da00000NOcmwIAD'</soql>
</LineItemsSOQL>}}-->
```

{% endcode %}

#### Merge fields

{% code title="solution-3-merge-fields.txt" %}

```
{{!myQuery2.quantity}}
{{!myQuery2.listprice}}
{{!myQuery2.totalPrice id="1" offset = "3"}}
```

{% endcode %}

#### Result

{% code title="solution-3-result.txt" %}

```
1.00
2.00
150.00
```

{% endcode %}

#### Special considerations

First define a filter for the records you want.

Then assign that filter an ID.

Finally, use `offset` to select which row from that filtered list to return.

### Solution 4: Return an aggregate value

Use this pattern when you need a total, count, or other aggregate result from SOQL.

#### Named query

{% code title="solution-4-named-query.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery3</queryname>
<soql>SELECT SUM(TotalPrice) asum FROM OpportunityLineItem WHERE opportunity.id= '006Da00000NOcmwIAD'</soql>
</LineItemsSOQL>}}-->
```

{% endcode %}

#### Merge field

{% code title="solution-4-merge-field.txt" %}

```
{{!myQuery3.asum}}
```

{% endcode %}

#### Result

{% code title="solution-4-result.txt" %}

```
430.0
```

{% endcode %}

#### Special considerations

Aggregate functions work in named queries.

Assign the aggregate result to an alias, then reference that alias in the merge field.


---

# 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/template-architecture/document-formats/pdf-upload-format/named-queries-for-pdf-upload-templates-reference.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.
