Query.apex provides a flexible and dynamic way of building a SOQL query on the Salesforce platform.
-
Allows both functional programming and object oriented programming paradigm
-
Manages object fields and provides Field Level Security checking
-
Supports complex queries including parent/child relationships, and nested conditions
This will return a list of all Accounts from the database.
By default it will select all the fields in Account object which the user has the access privilege on it.
List<Account> accounts = new Query('Account').run();
This will query the Accounts with a specific Id, and return only one SObject as a result.
Account account =
(Account)new Query('Account').
byIds('001O000001HMWZVIA5').
fetch(0);
This will query the Contacts given the AccountId foreign key
List<Contact> contacts =
new Query('Contact').
lookupIds('AccountId',
'001O000001HMWZVIA5').run();
This will select all the fields from the parent object Account.
List<Contact> contacts = new Query('Contact').selectParentFields('Account').run();
This will query all the accounts whose 'FirstName' is 'Sam' and 'LastName' is 'Tarly'.
By default, all the conditions are joined by 'AND' operator.
List<Account> accounts =
new Query('Account').
addConditionEq('FirstName', 'Sam').
addConditionEq('LastName', 'Tarly').
run();
For more complicated conditions, we can use the method 'conditionXX' to create a condition variable, and join these conditions with 'doOr' method or 'doAnd' method to join these condition variables.
List<Account> accounts =
new Query('Account').
addCondition(
Query.doAnd(
Query.doOr(
Query.conditionEq('FirstName', 'Sam'),
Query.conditionEq('Id', '001O000001HMWZVIA5')
),
Query.conditionLe('NumberOfEmployees', 15)
)
).
run();