-
Notifications
You must be signed in to change notification settings - Fork 0
The Soql.WhenClause Class
The Soql.WhenClause class is a builder for constructing WHEN clauses in TYPEOF queries. This class provides a fluent interface for specifying which fields to select when a particular SObjectType matches in a polymorphic field query.
This class is designed to work in conjunction with the Soql.TypeOf class and follows the builder pattern to enable method chaining.
Creates a new WhenClause builder instance. This constructor is private and should not be called directly. Instead, use the when() method on a Soql.TypeOf instance.
Parameters:
-
parent- The parent TypeOf instance -
objectType- The SObjectType for this WHEN clause
Specifies fields to select for this WHEN clause. All methods return the parent Soql.TypeOf instance to enable continued chaining.
Signatures:
Soql.TypeOf thenSelect(List<SObjectField> fields)Soql.TypeOf thenSelect(SObjectField field)Soql.TypeOf thenSelect(SObjectField field1, SObjectField field2)Soql.TypeOf thenSelect(SObjectField field1, SObjectField field2, SObjectField field3)Soql.TypeOf thenSelect(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4)Soql.TypeOf thenSelect(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)Soql.TypeOf thenSelect(List<Soql.Selectable> fields)Soql.TypeOf thenSelect(Soql.Selectable field)Soql.TypeOf thenSelect(Soql.Selectable field1, Soql.Selectable field2)Soql.TypeOf thenSelect(Soql.Selectable field1, Soql.Selectable field2, Soql.Selectable field3)Soql.TypeOf thenSelect(Soql.Selectable field1, Soql.Selectable field2, Soql.Selectable field3, Soql.Selectable field4)Soql.TypeOf thenSelect(Soql.Selectable field1, Soql.Selectable field2, Soql.Selectable field3, Soql.Selectable field4, Soql.Selectable field5)
Parameters:
-
fields- List of SObjectFields or Soql.Selectables to select for this WHEN clause -
field,field1,field2, etc. - Individual SObjectFields or Soql.Selectables to select (null values are automatically filtered out)
Returns:
- The parent
Soql.TypeOfinstance for continued method chaining
Features:
- Null Filtering: Automatically filters out null SObjectField and Soql.Selectable values
- Type Safety: Ensures only valid SObjectFields and Soql.Selectables are included
- Method Chaining: Returns parent TypeOf for continued fluent interface usage
- Polymorphic Support: Accepts both SObjectFields and Soql.Selectables (including ParentFields, Aggregations, etc.)
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(Account.Name);Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(Account.Name, Account.Phone, Account.Industry);List<SObjectField> accountFields = new List<SObjectField>{
Account.Name,
Account.Phone,
Account.Type,
Account.Industry,
Account.BillingCity
};
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(accountFields);Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(Account.Name, Account.Phone)
.when(Opportunity.SObjectType)
.thenSelect(Opportunity.Name, Opportunity.StageName, Opportunity.Amount)
.when(Contact.SObjectType)
.thenSelect(Contact.FirstName, Contact.LastName, Contact.Email)
.elseSelect('Name');// Using ParentField (cross-object fields)
Soql.ParentField parentField = new Soql.ParentField(Account.OwnerId, User.Name);
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(parentField, Account.Phone);// Combining SObjectFields and Soql.Selectables
Soql.ParentField ownerName = new Soql.ParentField(Account.OwnerId, User.Name);
List<Soql.Selectable> fields = new List<Soql.Selectable>{
Account.Name,
Account.Phone,
ownerName
};
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(fields);// Null fields are automatically filtered out
SObjectField nullField = null;
Soql.Selectable nullSelectable = null;
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(Account.Name, nullField, Account.Phone, nullSelectable); // null values are ignoredThe WhenClause class follows the Builder Pattern principles:
- Fluent Interface: Method chaining enables readable query construction
- Immutable Operations: Each method call returns the parent object for continued chaining
- Type Safety: Strong typing with SObjectField and Soql.Selectable parameters prevents runtime errors
- Defensive Programming: Automatic null filtering prevents invalid field references
- Polymorphic Field Support: Accepts multiple field types through the Soql.Selectable interface
Example of Fluent Chaining:
// Using mixed SObjectField and Soql.Selectable types
Soql.ParentField accountOwnerName = new Soql.ParentField(Account.OwnerId, User.Name);
Soql soql = DatabaseLayer.Soql.newQuery(Task.SObjectType)
.addSelect(
new Soql.TypeOf(Task.WhatId)
.when(Account.SObjectType)
.thenSelect(Account.Name, Account.Phone, accountOwnerName)
.when(Opportunity.SObjectType)
.thenSelect(Opportunity.Name, Opportunity.StageName)
.elseSelect('Name')
)
.addWhere(Task.Subject, Soql.NOT_EQUALS, null)
.toSoql();-
Soql.TypeOf- Parent class that creates WhenClause instances -
Soql.Builder- Main query builder that accepts TypeOf clauses -
Soql.Selectable- Interface implemented by TypeOf for builder compatibility
- Generating Test Records
- Dml
- Soql
- Cmdt
- Duplicates
- Plugins
- DatabaseLayer
- DatabaseLayerTestUtils
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- Duplicates
- MockDuplicates
- MockSoql
-
Soql
- Soql.AggregateResult
- Soql.Aggregation
- Soql.Binder
- Soql.Builder
- Soql.Condition
- Soql.ConditionalLogic
- Soql.Criteria
- Soql.Cursor
- Soql.Function
- Soql.InnerQuery
- Soql.InvalidParameterValueException
- Soql.LogicType
- Soql.NullOrder
- Soql.Operation
- Soql.Operator
- Soql.ParentField
- Soql.PreAndPostProcessor
- Soql.QueryLocator
- Soql.Request
- Soql.Scope
- Soql.Selectable
- Soql.SortDirection
- Soql.SortOrder
- Soql.Subquery
- Soql.TypeOf
- Soql.Usage
- Soql.WhenClause