Skip to content

The Soql.Subquery Class

Jason Siders edited this page Jul 4, 2025 · 6 revisions

Represents child relationship queries within the broader query structure. Used to return child objects related to the primary object.

This class implements Soql.Selectable, and can be used in conjunction with the addSelect builder method.

The object uses the Soql.Builder class to allow for flexible query construction. Once your query is built, call toSubquery() to build the query as a Soql.Subquery object:

SELECT Id, (SELECT Id FROM Contacts) FROM Account
Soql.Subquery sub = new Soql.Subquery(Contact.AccountId);
Soql soql = Database.Soql.newQuery(Account.SObjectType).addSelect(sub)?.toSoql();

Methods

This class inherits the Soql.Builder's query building methods, documented here.


Constructors:

  • Soql.Subquery(Schema.ChildRelationship relationship)
  • Soql.Subquery(SObjectField lookupFieldOnChildObject)
Clone this wiki locally