So we all know we can put an aggregate directly into an Integer, like this:

Integer countAccounts = [SELECT COUNT() FROM Account];

Is it possible to do the same thing, using Database.query()?

Integer countAccounts = Database.query('SELECT COUNT() FROM Account');

If so - how?


Yes. You can query using Database.countQuery(query)

String QueryString = 'SELECT count() FROM Account'; 
Integer countAccounts  = Database.countQuery(QueryString);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.