Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Count()

The COUNT() method in SOQL (Salesforce Object Query Language) is used to count the number of records that meet specified criteria. It’s similar to the COUNT() function in SQL and is useful when you need to know the total number of records without retrieving all of them.

COUNT() function returns the number of rows that match the filtering conditions. The resulting query result size field which returns the number of rows and the records will return null. This is important to understand.

So in short, COUNT() returns the number of items in a group, including NULL values and duplicates.

Below are some examples:

SELECT COUNT() FROM ObjectName

This will return the total number of records in the specified object.

SELECT COUNT() FROM ObjectName WHERE condition

This counts the number of records that meet the specified condition.

SELECT COUNT_DISTINCT(field_name) FROM ObjectName

This will count the number of unique values in the specified field.

Couple of important things to remember:

  • COUNT() should be the only element in the SELECT list. You can not add any other field along with COUNT().
  • COUNT() with a LIMIT clause is not allowed.
  • COUNT() with an ORDER BY clause is not allowed.
  • COUNT() with a GROUP BY clause is not allowed.

Count(fieldName)

In SOQL (Salesforce Object Query Language), the COUNT(fieldName) function is used to count the number of non-null (non-empty) values in a specific field across a set of records. This function differs from the COUNT() function, which simply counts the total number of records.

COUNT(fieldName) is particularly useful when you need to know how many records have a specified field filled in.

Again in short, COUNT(expression) evaluates an expression for each row in a group and returns the number of non-null values.

Here’s a bit more detail on how to use COUNT(fieldName):

SELECT COUNT(fieldName) FROM ObjectName

This counts the number of records where fieldName is not null in the specified object.

SELECT COUNT(fieldName) FROM ObjectName WHERE condition

This counts the number of records that meet the specified condition and have a non-null value in fieldName. Now Let’s understand few examples:

SELECT COUNT(Email) FROM Contact

This one will count the number of contacts with a non-empty email field.

SELECT COUNT(BillingCity) FROM Account WHERE BillingCity = 'San Francisco'

This one will count the number of accounts in a specific city.

Why Should We Use Count(fieldName) ?

We can include multiple COUNT(fieldName) items in a SELECT clause. 
For example, the below query returns the number of Cases, also returns  the number of Cases associated with an Account.

SELECT COUNT(Id), COUNT(AccountId) FROM Case

Since we can not use COUNT() with a GROUP BY, but we can use COUNT(fieldName) with a GROUP BY clause. 

For example, the below query returns the number of Cases for each Case Origin value :

SELECT COUNT(Id), Origin FROM Case GROUP BY Origin

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *