Getting started with Query.apex
Conditions
Most of the time, we want to make a query with conditions, typically querying a record with a specific Id or a lookup field, then we can use the 'addConditionEq' method:
Account account =
(Account)new Query('Account').
addConditionEq('Id', '0010l00000QJN3MAAX').
fetch();
This statement is querying an Account record with Id '0010l00000QJN3MAAX', equivalent to this statement:
Account account =
[ SELECT Id FROM Account WHERE Id = '0010l00000QJN3MAAX' ];
In previous tutorials we saw another statement which has the same functionality, using the 'byId' method:
Account account =
(Account)new Query('Account').
byId('0010l00000QJN3MAAX').
fetch();
Now let's try querying the accounts owned by the current user:
List accounts =
new Query('Account').
addConditionEq('OwnerId', UserInfo.getUserId()).
run();
equivalent to:
List accounts =
[ SELECT Id FROM Account WHERE OwnerId = :UserInfo.getUserId() ];
'addConditionEq(String field, Object arg)' is limiting the query with a field equals to the variable 'arg', while Query.apex provides other operators for conditions, including 'addConditionNotEq', 'addConditionIn', 'addConditionNotIn', 'addConditionLt', 'addConditionLe', 'addConditionGt', 'addConditionGe' and 'addConditionLike'.
Examples are:
new Query('Account').
addConditionNotEq('Name', 'N/A').
run();
new Query('Account').
addConditionIn('Name', new Set{'ABC'}).
run();
new Query('Account').
addConditionNotIn('Name', new Set{'N/A'}).
run();
new Query('Account').
addConditionLt('NumberOfEmployees', 15).
run();
new Query('Account').
addConditionLe('NumberOfEmployees', 10).
run();
new Query('Account').
addConditionGt('NumberOfEmployees', 5).
run();
new Query('Account').
addConditionGe('NumberOfEmployees', 10).
run();
new Query('Account').
addConditionLike('Name', '%ABC%').
run();