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 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.
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

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.
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

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')
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.



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.
If you wish to display the training session certifications, add a child object to the secondary object.

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.

If the main object is Account and we want to display the campaign members corresponding to the account's contacts.
In the case where you want to display all available Options-type products in Salesforce and add them to a table.

