# Convert a Component Query to the Optimized Query Model

Use this pattern when you want to pass queried fields into a component without running a separate query inside the component.

### Steps

{% stepper %}
{% step %}

### Update the component tag

* Add these attributes to your component tag:
  * `columnFields="true" mergeFieldPrefix="[your query object API name]"`
    {% endstep %}

{% step %}

### Pass each merge field as a column

* Include all fields in the `<soql>` query.
* Add a `<column componentMergeField="true">` entry for each field.
  {% endstep %}

{% step %}

### Update merge fields inside the component

* Every merge field in the component must start with your `mergeFieldPrefix`.
* Example: `{{!Account.Name}}` → `{{!Opportunity.Account.Name}}`
  {% endstep %}
  {% endstepper %}

### Example

Basic Component Query Model

```
<!--{{!<LineItemsSOQL>
<component>Component_Template</component>
<soql>SELECT Id FROM Opportunity WHERE AccountId='{{!Account.Id}}'</soql>
</LineItemsSOQL>}}-->
```

Optimized Component Query Model

```
<!--{{!<lineitemsSOQL>
  <class>none</class>
  <soql>SELECT Id, Account.Name, StageName FROM Opportunity WHERE AccountId='{{!Account.Id}}'</soql>
  <component columnFields="true" mergeFieldPrefix="Opportunity">Component_Template</component>
  <column componentMergeField="true">Id</column>
  <column componentMergeField="true">Account.Name</column>
  <column componentMergeField="true">StageName</column>
</lineitemsSOQL>}}-->
```

{% hint style="info" %}
Don’t forget to update the merge fields inside the component template.
{% endhint %}

### Using the Tags Field

If your component has many merge fields, `SDOC__Tags__c` makes this conversion much faster.

`SDOC__Tags__c` is simply a list of all merge fields in the template.

* Merge field syntax already stripped
* List is deduped
* Comma delimited
* Includes fields not visible from WYSIWYG view

Copy the whole field into a code/word editor and:

{% stepper %}
{% step %}

### 1. Check prefixes

* Ensure all fields begin with your merge field prefix.
* If needed, open the template editor and update the component merge fields.
  {% endstep %}

{% step %}

### 2. Remove base object from the merge fields

* Use find and replace to remove the base object and trailing `.` from the merge fields.
* Add the results directly to the Component Query `<soql>` tag.
  {% endstep %}

{% step %}

### 3. Wrap fields in tags

* Use find and replace on `,` to add opening and closing `<column>` tags.
* Then paste the full block into your Component Query.
  {% endstep %}
  {% endstepper %}

#### Note on the Merge Field Prefix

The merge field prefix can be any string.

Using the base object API name from the existing component is usually the easiest choice.

The prefix is the marker S-Docs uses to insert values from each returned row of the Component Query.

This includes `rownum`, so include it as a column in the query and as a prefixed merge field in the component when needed.


---

# 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/build-reusable-templates/component-template-how-to-article/using-component-queries/convert-a-component-query-from-method-1-to-method-2.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.
