Gonexa University

Custom Query (Secondary Object)

Custom queries allow you to navigate through the Salesforce object model without necessarily having to do with the original context.

Prerequisites
✓ Previous experience configuring a master model
✓ A master object already configured
✓ Proficiency in basic SOQL query concepts

What is it?

What is a custom query?

A custom query adds a second entry point to the data model, in addition to the main object. It allows you to access objects that have no direct link to it. This is useful in rare cases where the data model does not allow you to navigate to the desired data.

Why use a custom query?

Case study

When should you use a custom query?

Use it when you want to display data from an inaccessible object from the main object—that is, an object with no link to the main object.

When the primary object is Contact, some objects can be accessed without restriction, while others are isolated and unreachable:

✅ Their account (parent object)
✅ Their requests (child object)
✅ The account owner (parent object)
❌ The training session
❌ The account opportunities

DATA MODEL WITHOUT A CUSTOM OBJECT


Solution: Add a custom query to create a second starting point—essentially a new main object—that will allow you to access previously unreachable information.

Create a secondary object with only one result

Case #1

Example of custom queries linked to the main object

EXAMPLE CASE
Retrieve the decision-maker contact linked to my account

If 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 on “Custom query”
  • Choose the relevant Salesforce object. In our case, it is "Contact"
  • The label is populated by default, but you can modify it for better clarity in the template
  • Select "Only the first record"
  • Write the ID retrieval query. This SOQL query allows you to retrieve the desired record as follows:

How to write a query that returns only one result linked to the main object

The syntax SELECT Id FROM [object] WHERE [criteria1] AND [criteria2] allows you to retrieve a specific record that meets the defined criteria

Parameters:
[object] = the object the query is based on (e.g., Contact)
[criteria1] = mandatory criteria used to retrieve only records linked to the main object. It must link to the {RecordId} (e.g., AccountId = {RecordId}).
[criteria2] = (optional): additional criteria to more precisely target the desired record (e.g., Type__c = 'Decision Maker')


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

If you need to display information about a Training Session—an isolated object that is initially inaccessible—you can add a custom "Training Session" query that returns a single result, such as the date of the next training session.

DATA MODEL WITH A CUSTOM OBJECT
  • Go to the “Gonexa Assistant” component
  • Click on “Custom Request”
  • Select the relevant Salesforce object. In our case, this is "Training Session"
  • The label is populated by default, but you can edit it in the template for greater clarity.
  • Select "Only the first record."
  • Write the request to retrieve the ID, this SOQL query allows you to retrieve the record you want.

How to write a query that returns only one result

The syntax SELECT Id FROM [object] WHERE [criteria] allows you to retrieve a specific record that meets the defined criteria.

Parameters:
[object] = the object the query targets (e.g., Session__c)
[condition] = the condition used to retrieve the correct record (e.g., Name = 'Training')

Example
In our example, we want to retrieve the most recently created training session named "Gonexa Training." The query will be SELECT Id FROM Session__c WHERE Name = 'Gonexa Training' ORDER BY CreatedDate Asc

Tip
Use Salesforce Inspector to find the query that returns the correct record.


Create the child object for the "Training Session" custom request

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

DATA MODEL WITH A CUSTOM OBJECT AND A CHILD OBJECT
  • Still within the document template configuration, go to the "Gonexa Assistant" block and click on "Child object" below your custom query.
  • Configure the object as usual by following the table creation procedure, which is similar to that of the main object.


Create a custom query with multiple results

Case #2

In the case of a custom query with multiple results, the chosen object may or may not be linked to the main object, and must be in a table in the document.

  • Go to the Gonexa Wizard component, and click on “Custom Query”
  • Choose the Salesforce object concerned, the label is supplied by default but you can modify it for greater clarity in the model
  • Select “All records,” and a few additional fields will appear. If the object you wish to use is linked to the document template’s primary object, specify the connection between the two in the “Link to primary object” field. If there is no link, you can leave this field blank.
  • You can then add a filter and sorting for the records to be retrieved if necessary.


Example of a custom query linked to the main object

EXAMPLE CASE
Add a table to the Doc for a related object

If the main object is Account and we want to display the campaign members corresponding to the account's contacts. 

  • Choose the "Campaign Member" object
  • Select "All records"
  • In "Link to main object", navigate up the relationship to select "Account"
  • From the "Campaign Member" object, navigate up to the "Contact" object to reach the "Account"
  • Add a table to your document template and insert the tags into it


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

EXAMPLE CASE
Add a filtered table

In the case where you want to display all available Options-type products in Salesforce and add them to a table.

  • Choose the "Product" object
  • Select "All records"
  • Since we want to display all available products, leave the "Link to main object" field empty
  • In the "Filter" section, add a filter where type = "Options"
  • Add a table to your document template and insert the tags into it