> 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/enhanced-template-editor/advanced-data-using-soql-to-add-variables.md).

# Advanced Data: Using SOQL to Add Variables

While standard merge fields and related lists cover basic document generation needs, you can also incorporate advanced data into your S-Docs templates by leveraging Salesforce Object Query Language (SOQL). Building SOQL queries allows you to retrieve and display critical business data that lives outside the structure of your base record — such as pulling information from outside a standard opportunity tree hierarchy.

#### Accessing the advanced data panel

The advanced data configuration tools are located within the template editor interface. To access them:

1. Click the plus icon ![](/files/xWPAmanZr12xdJpqqHAj) to open the *Variables* panel. This panel serves as the primary entry point for all variable data.
2. Click the *Advanced Data* ![](/files/C34uUmNHB5Vdq5jcfwES) icon inside the *Variables* panel.

{% hint style="info" %}
The *Advanced Data* configuration panel will launch on the right side of the interface.
{% endhint %}

#### Understanding object queries versus list queries

The *Advanced Data* panel provides two options for constructing queries. While both tabs share identical configuration options, they handle your data differently:

* **Objects**: This option returns one instance of records defined in the SOQL query - only the first record that matches criteria set in the SOQL query. It is ideal when you want to make multiple fields from a single record available as individual merge fields within the *Variables* panel.
* **Lists**: This option returns multiple instances of records defined in the SOQL query. It is ideal when you need to query for multiple records - such as when you have a related list that requires data from multiple records.

#### Creating a SOQL data source

Follow these steps to build, validate, and inject a SOQL query into your document template:

{% columns %}
{% column width="41.66666666666667%" %}

<figure><img src="/files/4y0PJH15Mcr6nOcvoQtu" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="58.33333333333333%" %}

1. Open the *Variables* panel by clicking the plus icon ![](/files/xWPAmanZr12xdJpqqHAj) on the left side of the interface.
2. Click the *Advanced Data* ![](/files/C34uUmNHB5Vdq5jcfwES) icon to launch the configuration options in the right menu.
3. Select either the **Objects** tab or the **Lists** tab based on your data display needs.
4. Click on the **Click to add** **+** button to open the new query.
5. Enter a highly descriptive name in the **Label** field so template authors can easily identify its purpose.
6. Input your SOQL query into the query editor text box.
7. Select a valid record in the **Preview Record** field.
   {% endcolumn %}
   {% endcolumns %}

{% hint style="info" %}
Note: The **Add Data Source** button remains inactive until you select a preview record. It is required to preview record data to validate the query syntax and confirm that the query is returning accurate data.
{% endhint %}

8. Review the **Results** and **Fields** tabs to verify your query configuration.

{% hint style="info" %}
This tab displays the data type, label, and API name to confirm your SOQL query is well-formed, even if your chosen preview record is currently missing data for those specific fields.

Note: Malformed SOQL will return an invalid error that contains a description of the errant syntax.
{% endhint %}

9. Click **Add Data Source**.

{% hint style="info" %}
Need to make an update to a SOQL query? Simply navigate to the Advanced Data panel and click on the listed query when the ![](/files/C34uUmNHB5Vdq5jcfwES) icon is visible.
{% endhint %}

#### Result

Once you save a valid query definition, those fields automatically become available in the *Variables* panel for immediate use across your document template. The queried data will appear grouped under a new subcategory named after the configured SOQL data element.


---

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.sdocs.com/sdocs/enhanced-template-editor/advanced-data-using-soql-to-add-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
