- 
                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)
Parameters:
- 
fields- List of SObjectFields to select for this WHEN clause - 
field,field1,field2, etc. - Individual SObjectFields 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 values
 - Type Safety: Ensures only valid SObjectFields are included
 - Method Chaining: Returns parent TypeOf for continued fluent interface usage
 
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');// Null fields are automatically filtered out
SObjectField nullField = null;
Soql.TypeOf typeOfClause = new Soql.TypeOf(Task.WhatId)
    .when(Account.SObjectType)
        .thenSelect(Account.Name, nullField, Account.Phone); // nullField is 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 parameters prevents runtime errors
 - Defensive Programming: Automatic null filtering prevents invalid field references
 
Example of Fluent Chaining:
Soql soql = DatabaseLayer.Soql.newQuery(Task.SObjectType)
    .addSelect(
        new Soql.TypeOf(Task.WhatId)
            .when(Account.SObjectType)
                .thenSelect(Account.Name, Account.Phone)
            .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
 - Plugins
 
- DatabaseLayer
 - Dml
 - MockDml
 - MockRecord
 - Cmdt
 - MockCmdt
 - 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