Query.apex provides a flexible and dynamic way of building a SOQL query on the Salesforce platform.
Although Salesforce provides Database.query method to dynamically execute a query from a string, it is far from easy to construct such a string in a structural and flexible way. Query.apex is made to improve the flexibility of the code and consequently enhance the productivity of the development.
-
Allows both functional programming and object oriented programming paradigm
-
Supports complex queries including parent/child relationships, and nested conditions in a flexible way
-
Manages the namespace of the object names and field names, while also providing the Field Level Security checking
https://click-to-cloud.github.io/query-apex/
This will return a list of all Accounts from the database.
By default it will select only the Id field.
List<Account> accounts = new Query('Account').run();
This will query all Accounts from the database, selecting all fields which the user has access privilege on.
List<Account> accounts =
new Query('Account').
selectAllFields().
run();
This will query all Accounts from the database, selecting specified fields only.
List<Account> accounts =
new Query('Account').
selectField('Name').
selectFields('CreatedById').
selectFields('CreatedDate, LastModifiedDate').
selectFields(new List<String>{'LastActivityDate', 'LastViewedDate'}).
run();
This will query the Accounts with a specific Id, and return only one SObject as a result.
Account account =
(Account)new Query('Account').
byId('001O000001HMWZVIA5').
fetch();
This will query the Contacts given the foreign key AccountId.
List<Contact> contacts =
new Query('Contact').
lookup('AccountId', '001O000001HMWZVIA5').
run();
This will query all the Accounts and return a list of Id as a result.
List<Account> accounts =
new Query('Account').
toIdList();
This will select all the fields from the parent object Account.
List<Contact> contacts =
new Query('Contact').
selectAllFields('Account').
run();
This will query all the accounts whose 'FirstName' is 'Sam' and 'LastName' is 'Tarly'.
By default, all the conditions are joined by the 'AND' operator.
List<Account> accounts =
new Query('Account').
addConditionEq('Name', 'Sam').
addConditionLt('NumberOfEmployees', 10).
run();
For more complicated conditions, we can use the method 'conditionXX' to create a condition variable, before using the 'doOr' method or 'doAnd' boolean operation methods to join these conditions.
List<Account> accounts =
new Query('Account').
addCondition(
Query.doAnd(
Query.doOr(
Query.conditionEq('FirstName', 'Sam'),
Query.conditionEq('Id', '001O000001HMWZVIA5')
),
Query.conditionLe('NumberOfEmployees', 15)
)
).
run();
Query.apex also allows selecting child relationships (subqueries), in a functional style similar to the conditions.
List<Account> accounts =
new Query('Account').
addSubquery(
Query.subquery('Contacts').
addConditionEq('FirstName', 'Sam').
addConditionIn('LastName', new List<String>{'Tarly'})
).
run();