• 1Installation
  • 2Simple queries
  • 3Fields Selection
  • 4Fields Selection Continued
  • 5Conditions
  • 6Conditions Part 2
  • 7Conditions Part 3
  • 8Subqueries
  • 9Aggregate Functions Part 1
  • 10Aggregate Functions Part 2
  • 11Aggregate Functions Part 3

Query.apex

  • Docs
  • Tutorials
Getting started with Query.apex

Conditions Part 2

In the previous section, we have learned to add a single condition to the query. In many cases, however, that is far from enough. So Query.apex allows calling 'addConditionXX' multiple times, resulting in combining all the conditions with boolean 'and' operation.

Example:

List accounts =
    new Query('Account').
    addConditionEq('Name', 'Sam').
    addConditionGt('NumberOfEmployees', 0).
    addConditionIn('Phone', new Set{'+61 400 000 000'}).
    run();

equivalent to:

List accounts =
    [ SELECT Id FROM Account
      WHERE Name = 'Sam'
      AND NumberOfEmployees > 0
      AND Phone IN :new Set{'+61 400 000 000'} ];

Meanwhile, Query.apex provides a method 'switchToDisjunction' to change the boolean 'and' operator to the boolean 'or' operator. Appending an extra 'switchToDisjunction' method to the same example above:

List accounts =
    new Query('Account').
    addConditionEq('Name', 'Sam').
    addConditionGt('NumberOfEmployees', 0).
    addConditionIn('Phone', new Set{'+61 400 000 000'}).
    switchToDisjunction().
    run();

equivalent to:

List accounts =
    [ SELECT Id FROM Account
      WHERE Name = 'Sam'
      OR NumberOfEmployees > 0
      OR Phone IN :new Set{'+61 400 000 000'} ];
Done