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, andLIMITTest 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
OpportunityAt least one
OpportunityLineItemrecord on your test opportunityBasic familiarity with merge fields such as
{{!Opportunity.Name}}
If you are brand new to S-Docs syntax, complete a basic merge field tutorial first.
Step 1: Open or Create Your Template
Start with an Opportunity template where you want to show data outside a table.
Navigate to S-Docs Templates in Salesforce
Open an existing template or click New Template
Choose
Opportunityas the related objectOpen the template editor
Click Source if you want to paste the query directly
You now have a place to add a named query.
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 tableUsed
<queryname>TopLineItem</queryname>to store the result under one nameUsed
<soql>to return one line item for the current opportunity
How to read it:
WHERE OpportunityId = '{{!Opportunity.Id}}'ties the query to the current recordORDER BY TotalPrice DESCsorts the largest line item firstLIMIT 1returns only that first row
The query name is case-sensitive.
Use the exact same value later in your merge fields.
Step 3: Reuse the Returned Values
Now place merge fields anywhere below the query block.
What you just did:
Referenced the named query with
TopLineItemPulled 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.
Step 4: Understand What Makes the Output Work
Named queries only expose fields that you include in the SELECT clause.
In this example:
TopLineItemcomes from<queryname>Name,Quantity, andTotalPricecome fromSELECTThe 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:
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 ASCsorts from smallest to largestLowestLineItemcreates a second reusable query result
Use WHERE, ORDER BY, and LIMIT together when you need one specific row.
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.
Step 7: Save and Test Your Template
Now test the query with a real opportunity.
Click Save in the template editor
Generate the template from an opportunity with line items
Confirm the highest-value line item appears
Compare the output against the actual line items on the opportunity
What to verify:
The template runs from
OpportunityThe opportunity has at least one line item
The query includes every field you reference later
The query name matches your merge fields exactly
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
SELECTclauseMake 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
WHEREclause firstCheck the
ORDER BYdirection,ASCorDESCUse
LIMIT 1when you only want one rowTest 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
SELECTUse 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:
A named query for the highest-value
OpportunityLineItemThe line item name, quantity, and total price in plain text
One repeated use of the same query result in a second section
A variation that returns the lowest-priced line item instead
A test using one opportunity that has line items and one that does not
Bonus challenges:
Add a relationship field such as
PricebookEntry.Product2.NameAdd another field to the same query and output it later
Create a second named query with a different filter
Last updated
Was this helpful?

