For the complete documentation index, see llms.txt. This page is also available as Markdown.

Advanced Data Pulling (For Admins)

Define a named query once, then reuse the returned values anywhere in your template.

What You'll Learn

In this tutorial, you'll build a simple quote summary that pulls one reusable result from a SOQL query. By the end, you'll know how to:

  • Create a named query with <queryname>

  • Return data without rendering a table

  • Reuse query results with {{!QueryName.FieldName}}

  • Control which row is returned with WHERE, ORDER BY, and LIMIT

  • Test and troubleshoot blank named query output

What You'll Build

An Opportunity template that includes:

  • The highest-value line item name

  • The quantity for that line item

  • The total price for that line item

  • Reusable values you can place in the body, header, or footer

Estimated time: 20 minutes

Prerequisites

Before starting, ensure you have:

  • Access to S-Docs Templates in Salesforce

  • Permission to create or edit templates

  • An S-Docs template related to Opportunity

  • At least one OpportunityLineItem record on your test opportunity

  • Basic familiarity with merge fields such as {{!Opportunity.Name}}

If you are brand new to S-Docs syntax, complete a basic merge field tutorial first.

1

Step 1: Open or Create Your Template

Start with an Opportunity template where you want to show data outside a table.

  1. Navigate to S-Docs Templates in Salesforce

  2. Open an existing template or click New Template

  3. Choose Opportunity as the related object

  4. Open the template editor

  5. Click Source if you want to paste the query directly

You now have a place to add a named query.

2

Step 2: Add Your First Named Query Block

Named queries use a SOQL block plus a reusable query name.

Add this block near the top of your template:

What you just did:

  • Used <class>none</class> to stop S-Docs from rendering a table

  • Used <queryname>TopLineItem</queryname> to store the result under one name

  • Used <soql> to return one line item for the current opportunity

How to read it:

  • WHERE OpportunityId = '{{!Opportunity.Id}}' ties the query to the current record

  • ORDER BY TotalPrice DESC sorts the largest line item first

  • LIMIT 1 returns only that first row

3

Step 3: Reuse the Returned Values

Now place merge fields anywhere below the query block.

What you just did:

  • Referenced the named query with TopLineItem

  • Pulled field values from the query result

  • Formatted the total as currency

The merge field pattern is:

{{!QueryName.FieldName}}

If the query name or field name does not match, the output will be blank.

4

Step 4: Understand What Makes the Output Work

Named queries only expose fields that you include in the SELECT clause.

In this example:

  • TopLineItem comes from <queryname>

  • Name, Quantity, and TotalPrice come from SELECT

  • The query block must appear before the merge fields that use it

If you want to output another field later, add it to the query first.

Example:

Then you can use:

5

Step 5: Change Which Record the Query Returns

Named queries are useful because you control exactly which row is stored.

Try this variation if you want the lowest-priced line item instead:

What changed:

  • ORDER BY TotalPrice ASC sorts from smallest to largest

  • LowestLineItem creates a second reusable query result

Use WHERE, ORDER BY, and LIMIT together when you need one specific row.

6

Step 6: Use the Same Query Result in Multiple Places

Once a named query runs, you can reuse it anywhere later in the template.

Example:

What you just did:

  • Ran the SOQL once

  • Reused the same values several times

  • Kept the template shorter and easier to maintain

This is the main reason to use named queries instead of repeating logic.

7

Step 7: Save and Test Your Template

Now test the query with a real opportunity.

  1. Click Save in the template editor

  2. Generate the template from an opportunity with line items

  3. Confirm the highest-value line item appears

  4. Compare the output against the actual line items on the opportunity

What to verify:

  • The template runs from Opportunity

  • The opportunity has at least one line item

  • The query includes every field you reference later

  • The query name matches your merge fields exactly

8

Step 8: Experiment with Variations

Once the basics work, try a few common variations.

Variation 1: Return a relationship field

Then output:

Variation 2: Filter to a smaller set of rows

Variation 3: Add another field to the same query

Then output:

Try one variation at a time, then regenerate the document to confirm the result.

Common Issues and Solutions

Issue 1: "The merge fields are blank"

Problem: The named query block runs, but the output fields do not show values.

Solution:

  • Verify the query actually returns a row for the current record

  • Check that the query name matches exactly, including capitalization

  • Confirm every referenced field is included in the SELECT clause

  • Make sure the merge fields appear below the named query block

Issue 2: "A table shows up instead of plain text"

Problem: The query output renders like a related list.

Solution:

  • Make sure the block uses <class>none</class>

  • Check that you did not replace it with a table class name

  • Regenerate the document after saving the change

Issue 3: "The wrong row is returned"

Problem: You expected one record, but a different row appears.

Solution:

  • Review the WHERE clause first

  • Check the ORDER BY direction, ASC or DESC

  • Use LIMIT 1 when you only want one row

  • Test the SOQL logic against real record data

Issue 4: "A relationship field is blank"

Problem: A field like product name does not show in the output.

Solution:

  • Include the full relationship path in SELECT

  • Use the same full path in the merge field

  • Verify the related record actually exists on that row

Issue 5: "The query works in one place but not another"

Problem: Some references resolve, but earlier ones do not.

Solution:

  • Place the named query before every section that uses it

  • Avoid referencing the query result above the query block

  • Keep the query block near the top of the template when possible

What You've Learned

Congratulations! You've built a template with a reusable named query and learned:

✅ How to create a named query with <queryname> ✅ How to prevent table output with <class>none</class> ✅ How to reuse query values with {{!QueryName.FieldName}} ✅ How to control the returned row with WHERE, ORDER BY, and LIMIT ✅ How to test and troubleshoot blank output

Next Steps

Now that you've mastered the basics, you can:

Expand Your Queries

  • Pull relationship fields into your output

  • Return a different row with sorting and filters

  • Add more fields to one reusable query

  • Build multiple named queries in one template

Combine with Other Features

  • Use named queries with conditional logic

  • Pair named queries with calculations

  • Reuse named query output inside component templates

  • Mix named queries with standard merge fields

Practice More

  • Build a quote summary for the top-priced line item

  • Show a service date only for the returned line item

  • Add a second named query for a different product row

Recommended reading:

  • Named Query Working Examples

  • How to: Advanced Data Retrieval with Named Queries

  • Named Query Structure

  • Named Queries In Microsoft Templates (DOCX, PPTX, XLSX)

Practice Exercise

To reinforce what you've learned, build an Opportunity template that includes:

  1. A named query for the highest-value OpportunityLineItem

  2. The line item name, quantity, and total price in plain text

  3. One repeated use of the same query result in a second section

  4. A variation that returns the lowest-priced line item instead

  5. A test using one opportunity that has line items and one that does not

Bonus challenges:

  • Add a relationship field such as PricebookEntry.Product2.Name

  • Add another field to the same query and output it later

  • Create a second named query with a different filter

Last updated

Was this helpful?