• 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

Simple queries

Lets start with a simplest query: querying all Account records:

List accounts = new Query('Account').run();

The 'run' method executes the query and returns the type 'List'.

This is equivalent to this statement, selecting only the ID field in the Account records.

List accounts = [ SELECT Id FROM Account ];

We can now move further by querying an Account record with a specific Id, which is quite an common case in development.

Account account =
    (Account)new Query('Account').
    byId('0010l00000QJN3MAAX').
    fetch();

The 'byId' method limits the result with a specific Id.

The 'fetch' method executes the query and returns the first record in the result.

The statement is equivalent to:

Account account =
    [ SELECT Id FROM Account WHERE Id = '0010l00000QJN3MAAX' ];

That's our first tutorial of Query.apex. We just learned to build a simple query from Query.apex.

Done