Skip to main content
Version: 6.13.0

TypeOf

Construct TYPEOF expression with provided API.

SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Phone, Account.NumberOfEmployees)
.whenElse('Name')
)
.toList();

Methodsโ€‹

The following are methods for TypeOf.

INIT

WHEN

THEN

ELSE

INITโ€‹

ofโ€‹

Constructs a TypeOf for a polymorphic relationship field. Pass the lookup field (Event.WhatId) or the relationship name ('What').

Signature

TypeOf of(String ofField)
TypeOf of(SObjectField ofField)

Example

SELECT TYPEOF What
WHEN Account THEN Phone
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Phone)
)
.toList();

WHENโ€‹

when objectโ€‹

Starts a WHEN branch for the object type. The following then methods add fields to that branch.

Signature

TypeOf when(SObjectType whenObject)

Example

SELECT TYPEOF What
WHEN Account THEN Phone, NumberOfEmployees
WHEN Opportunity THEN Amount, CloseDate
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Phone, Account.NumberOfEmployees)
.when(Opportunity.SObjectType)
.then(Opportunity.Amount, Opportunity.CloseDate)
)
.toList();

THENโ€‹

then field1 - field5โ€‹

Signature

TypeOf then(SObjectField field)
TypeOf then(SObjectField field1, SObjectField field2)
TypeOf then(SObjectField field1, SObjectField field2, SObjectField field3)
TypeOf then(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4)
TypeOf then(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)

Example

SELECT TYPEOF What
WHEN Account THEN Id, Name, Phone
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Id, Account.Name, Account.Phone)
)
.toList();

then fieldsโ€‹

Use for more than 5 fields.

Signature

TypeOf then(List<SObjectField> fields)

Example

SELECT TYPEOF What
WHEN Account THEN Id, Name, Phone, Industry, BillingCity, BillingCountry
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(new List<SObjectField>{
Account.Id,
Account.Name,
Account.Phone,
Account.Industry,
Account.BillingCity,
Account.BillingCountry
})
)
.toList();

then string fieldsโ€‹

NOTE! With String Apex does not create reference to field. Use SObjectField whenever it possible. Method below should be only use for dynamic queries.

Signature

TypeOf then(Iterable<String> fields)
TypeOf then(String fields)

Example

SELECT TYPEOF What
WHEN Account THEN Id, Name, Owner.Name
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then('Id, Name, Owner.Name')
)
.toList();

Allows to add parent field.

Signature

TypeOf then(String relationshipName, SObjectField field)
TypeOf then(String relationshipName, SObjectField field1, SObjectField field2)
TypeOf then(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3)
TypeOf then(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4)
TypeOf then(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)

Example

SELECT TYPEOF What
WHEN Opportunity THEN Account.Id, Account.Name
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Opportunity.SObjectType)
.then('Account', Account.Id, Account.Name)
)
.toList();

Allows to add parent fields.

Use for more than 5 parent fields.

Signature

TypeOf then(String relationshipName, Iterable<SObjectField> fields)

Example

SELECT TYPEOF What
WHEN Opportunity THEN Account.Id, Account.Name, Account.Phone, Account.Industry, Account.BillingCity, Account.BillingCountry
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Opportunity.SObjectType)
.then('Account', new List<SObjectField>{
Account.Id,
Account.Name,
Account.Phone,
Account.Industry,
Account.BillingCity,
Account.BillingCountry
})
)
.toList();

ELSEโ€‹

else fieldsโ€‹

Fields returned when no WHEN branch matches. Salesforce allows only fields of the Name object here, e.g. Name, Email, Phone.

Signature

TypeOf whenElse(Iterable<String> fields)
TypeOf whenElse(String fields)

Example

SELECT TYPEOF What
WHEN Account THEN Phone
ELSE Name, Email
END
FROM Event
SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Phone)
.whenElse('Name, Email')
)
.toList();