Concepts or coding lessons of Salesforce that you can implement easily

Difference Between Count() And Count(fieldname) In Query Secrets Revealed

how to do it in salesforce
COUNT() :



COUNT() is an optional clause that can be used in a SELECT statement in a SOQL query to discover the number of rows that a query returns.

This function returns the number of rows that match the filtering conditions and COUNT() must be the only element in the select list. The resulting query result size field which returns the number of rows and the records will return null.

In simple words, COUNT() returns the number of items in a group, including NULL values and duplicates.


For example:

SELECT COUNT() FROM Account WHERE Name LIKE 'a%'

SELECT COUNT() FROM Contact, Contact.Account WHERE Account.Name = 'Tester tube'

For COUNT(), the query result size field returns the number of rows. The records field returns null.

Note the following when using COUNT():
  • COUNT() must be the only element in the SELECT list. that is you can not add any other field with count()
for example : SELECT COUNT(), id FROM Account WHERE Name LIKE 'xyz%'
This SOQL give Unknown error parsing query
  • You can use COUNT() with a LIMIT clause.
  • You can not use COUNT() with an ORDER BY clause. Instead of that Use COUNT(fieldName) .
  • You can not use COUNT() with a GROUP BY clause for API version 19.0 and later. Instead of that Use COUNT(fieldName) .



COUNT(fieldname) :


This function returns the number of rows that match the filtering conditions and have a non-null value records. An Aggregate Result object in the records field contains the number of rows. Do not use the size field for the resulting records.

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

So count() includes nulls, the other method doesn't.

For Example:

SELECT COUNT(Id) FROM Account WHERE Name LIKE 'xyz%'

Note: COUNT(Id) in SOQL is equivalent to COUNT(*) in SQL.

Advantages to using COUNT(fieldName) :


We have below advantages to using COUNT(fieldName) alternative of COUNT() :

1. You can include multiple COUNT(fieldName) items in a SELECT clause. 
For example, the below query returns the number of opportunities, also returns  the number of opportunities associated with a campaign.

SELECT COUNT(Id), COUNT(CampaignId) FROM Opportunity

2. As you know that we can not use COUNT() with a GROUP BY clause for API version 19.0 and later. But in COUNT(fieldName) with a GROUP BY clause for API version 18.0 and later. 
This allows you to discover your records and return summary reporting information. 
For example, the below query returns the number of leads for each LeadSource value :

SELECT COUNT(Name), LeadSource FROM Lead GROUP BY LeadSource


More Salesforce Blogs:

3 Easy Steps To Send Emails With Attachment From Your Apex Class In Salesforce
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind

Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next post: The Ultimate Guide To Get Sobject Record Type Id By Record Type Name Without SOQL
loading...

3 comments:

  1. centered and descriptive articles written in this blog is surely very helpful for me similarly to for different who looking for such type of expertise. it is in reality going to grow to be useful in coming destiny.coaching amsterdam

    ReplyDelete
  2. Mesmerized article written on this blog with other relevant information. It is straight to the point that how we can improve our skills as well as how we can be represented to a new stream of professionalism.
    persoonlijke ontwikkeling amsterdam

    ReplyDelete
  3. This is one awesome blog article. Much thanks again salesforce certification

    ReplyDelete