function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Aggregate to get record count

Hello All,'
Trying to get count of records that has the same matching field value as the record being saved.  Tried mapping but could never get teh correct value.  Will someone assist with geving an example of using the aggregate function to ge tthe count?  Example record being created has a text field Test__c and a value of dog.  Need to find all of the existing records with the value dog in the Test__c  field.
Thanks you for your time,
P
Best Answer chosen by Phuc Nguyen 18
AnudeepAnudeep (Salesforce Developers) 
To discover the number of rows that a query returns, use the aggregate function COUNT() in a SELECT statement of a SOQL query.

Use one of the following forms of syntax for COUNT():
  • COUNT()
  • COUNT(fieldName)
If you are using a GROUP BY clause, use COUNT(fieldName) instead of COUNT().

Here are examples
 
SELECT COUNT()
FROM Account
WHERE Name LIKE 'a%'
 
SELECT COUNT()
FROM Contact, Contact.Account
WHERE Account.Name = 'MyriadPubs'

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!