Skip to main content
Version: 6.13.0

TYPEOF

For more details check SOQL API - TypeOf.

NOTE! 🚨 All examples use inline queries built with the SOQL Lib Query Builder. If you are using a selector, replace SOQL.of(...) with YourSelectorName.query().

SOQL

SELECT TYPEOF What
WHEN Account THEN Phone, NumberOfEmployees
WHEN Opportunity THEN Amount, CloseDate
ELSE Name, Email
END
FROM Event

SOQL Lib

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)
.whenElse('Name, Email')
)
.toList();

Fields​

SOQL

SELECT Subject, TYPEOF What
WHEN Account THEN Name, Phone
END
FROM Event

SOQL Lib

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

String Fields​

SOQL

SELECT Subject, TYPEOF What
WHEN Account THEN Name, Phone
END
FROM Event

SOQL Lib

SOQL.of(Event.SObjectType)
.with(Event.Subject)
.with(SOQL.TypeOf.of('What')
.when(Account.SObjectType)
.then('Name, Phone')
)
.toList();

Parent Fields​

SOQL

SELECT TYPEOF What
WHEN Opportunity THEN Name, Account.Name, Account.Phone
END
FROM Event

SOQL Lib

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

Multiple TYPEOF​

SOQL

SELECT
TYPEOF What
WHEN Account THEN Name
END,
TYPEOF Who
WHEN Contact THEN Email
END
FROM Event

SOQL Lib

SOQL.of(Event.SObjectType)
.with(SOQL.TypeOf.of(Event.WhatId)
.when(Account.SObjectType)
.then(Account.Name)
)
.with(SOQL.TypeOf.of(Event.WhoId)
.when(Contact.SObjectType)
.then(Contact.Email)
)
.toList();