Skip to main content

WHERE

Use SOQL.FilterGroup and Use SOQL.Filter to build your WHERE clause.

SELECT Id, Name
FROM Account
WHERE Id = :accountId OR Name LIKE :'%' + accountName + '%'
public inherited sharing class SOQL_Account implements SOQL.Selector {

public static SOQL query()
return SOQL.of(Account.SObjectType)
.with(Account.Id, Account.Name);
}
}

public with sharing class MyController {

public static List<Account> getByIdOrName(Id accountId, String accountName) {
return SOQL_Account.query()
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.id().equal(accountId))
.add(SOQL.Filter.name().contains(accountName))
.anyConditionMatching() // OR
)
.toList();
}
}

Custom Order

SELECT Id
FROM Account
WHERE (Name = 'My Account' AND NumberOfEmployees >= 10)
OR (Name = 'My Account' AND Industry = 'IT')
SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.name().equal('My Account'))
.add(SOQL.Filter.with(Account.NumberOfEmployees).greaterOrEqual(10))
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
.conditionLogic('(1 AND 2) OR (1 AND 3)')
).toList();

Dynamic Filters

SELECT Id
FROM Account
WHERE
Industry = 'IT' AND
Name = 'My Account' AND
NumberOfEmployees >= 10
// build conditions on fly
SOQL.FilterGroup group = SOQL.FilterGroup
.add(SOQL.Filter.name().equal('My Account'))
.add(SOQL.Filter.with(Account.NumberOfEmployees).greaterOrEqual(10));

SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
.add(group)
).toList();