> For the complete documentation index, see [llms.txt](https://university.gonexa.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university.gonexa.fr/doc/en/advanced-features/custom-query/custom-query-single-result.md).

# Create a custom query with a single result

Retrieve the decision-maker contact linked to my account

## Example of a custom query linked to the main object

**Retrieve the decision-maker contact linked to my account**

The main object is Account, and we want to retrieve the decision-maker contact to display their first and last name in the generated document

* From your document template, go to the "Gonexa Assistant" component
* Click "Custom query"
* Choose the relevant Salesforce object. In our case, it is "Contact"
* The label is filled in by default, but you can change it for more clarity in the template
* Select "Only the first record"
* Write the query that retrieves the ID. This SOQL query lets you retrieve the record you want, as follows:

![Custom query on Contact](https://4198981723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTYUzkIngX1MnHE8kOJDR%2Fuploads%2FVUFYsu4aEmrLd7WMfVlQ%2Frequete-personnalisee-2.png?alt=media)

**How to write a query that returns a single result linked to the main object**

The syntax `SELECT Id FROM [objet] WHERE [critère1] AND [critère2]` retrieves a specific record that meets the defined criteria

**Parameters:**

* `[objet]` = the object the query is based on (e.g. `Contact`)
* `[critère1]` = **mandatory** criterion used to retrieve only the records linked to the main object. It must link to the `{RecordId}` (e.g. `AccountId = {RecordId}`).
* `[critère2]` = *(optional)*: additional criterion used to target the desired record more precisely (e.g. `Type__c = 'Décisionnaire'`)

## Example of a custom query with no link to the main object

You want to display information about the Training Session, an isolated object that is initially unreachable. Add a "Training Session" custom query that returns a single result, such as the date of the next training session.

**Data model with a custom object**

![Data model with a custom object](https://4198981723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTYUzkIngX1MnHE8kOJDR%2Fuploads%2FJi7PZkONvKd91yuqfQhh%2Frequete-personnalisee-3.png?alt=media)

* From your document template, go to the "Gonexa Assistant" component
* Click "Custom query"

![Gonexa Assistant - Custom query](https://4198981723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTYUzkIngX1MnHE8kOJDR%2Fuploads%2FIYq47mXU7TxVoNEGBEhJ%2Frequete-personnalisee-4.png?alt=media)

* Choose the relevant Salesforce object. In our case, it is "Training Session"
* The label is filled in by default, but you can change it for more clarity in the template
* Select "Only the first record"
* Write the query that retrieves the ID. This SOQL query lets you retrieve the record you want, as follows:

![Custom query on Training Session](https://4198981723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTYUzkIngX1MnHE8kOJDR%2Fuploads%2FRnPpSfpeKs8wSlVLGdQW%2Frequete-personnalisee-5.png?alt=media)

**How to write a query that returns a single result with no link to the main object**

The syntax `SELECT Id FROM [objet] WHERE [critère]` retrieves a specific record that meets the defined criterion

**Parameters:**

* `[objet]` = the object the query is based on (e.g. `Session__c`)
* `[critère]` = criterion used to retrieve the right record (e.g. `Name = 'Formation'`)

{% hint style="success" %}
**Example**

In our example, we want to retrieve the most recently created training session named "Formation Gonexa". The query is:

```
SELECT Id FROM Session__c WHERE Name = 'Formation Gonexa' ORDER BY CreatedDate Asc
```

{% endhint %}

{% hint style="warning" %}
**Tip**

Use Salesforce Inspector to find the query that returns the right record.
{% endhint %}

## Create the child object of the "Training Session" custom query

If you want to display the certifications of the training session, add a child object to the secondary object.

**Data model with a custom object and a child object**

![Data model with a custom object and a child object](https://4198981723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTYUzkIngX1MnHE8kOJDR%2Fuploads%2FN9HJTXGD6I70xHXv5eIX%2Frequete-personnalisee-6.png?alt=media)

* Still in the document template configuration, in the "Gonexa Assistant" block, click "Child object" below your custom query
* Configure the object as usual by following the table creation procedure, which is the same as for the main object


---

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://university.gonexa.fr/doc/en/advanced-features/custom-query/custom-query-single-result.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
