0

I'm trying to figure out the best way to get a count of the number of kids per location.

Here is the current query that works and populates my VF page.

SELECT puLocation__r.Name LocationName, 
                    puLocation__r.notes__c LocationNotes,
                    puLocation__r.Milk_Location__c MilkLocation,
                    puLocation__r.Location_type__c LocationType
             FROM Child_Consent_Form__c 
             GROUP BY puLocation__r.Name, 
                      puLocation__r.notes__c,
                      puLocation__r.Milk_Location__c,
                      puLocation__r.Location_type__c];

This query below worked in the query editor but the forum helped me figure out that this wasn't going to work on my VisualForce page because of the count(). The count(Name) gave me the total number of kids per location.

//Records = [SELECT puLocation__r.Name, puLocation__r.Milk_Location__c, puLocation__r.notes__c, COUNT(Name) from Child_Consent_Form__c GROUP BY puLocation__r.Name, puLocation__r.Milk_Location__c, puLocation__r.notes__c ]; 

Here is the original thread.

Unknown property 'SObject. - Why can't I display the object?

My problem is that I can't figure out how to get around this.

4
  • 7
    This is part of why people are encouraged to wait for a decent amount of time before accepting an answer. On your first question, David Reed's comment on his answer nails it. To quote (or as best I can do here in the comments): Generally speaking, you can do SELECT Name, (SELECT Id FROM Child_Objects__r) FROM Parent_Object__c WHERE..., and then in Visualforce obtain the count with {! parent_object.Child_Objects__r.size } Commented Dec 27, 2017 at 20:35
  • Check this link salesforce.stackexchange.com/questions/84256/… Commented Dec 27, 2017 at 20:39
  • You should really take at least a few minutes to Read The Manual. Salesforce has thorough and clear documentation about its query syntax. That includes how Aggregate Functions work. Neglecting to research well documented features very often leads to down-votes, as you have seen here. Commented Dec 27, 2017 at 21:09
  • Hi Adrian - Thank you for pointing that out. I thought that I was being smart and opening another thread. The goal was to get Derek more click points. My bad. At some point, I hope to support the community. Right now I am trying to understand why this isn't working. Commented Dec 28, 2017 at 11:05

1 Answer 1

3

Your question is very clearly answered in the documentation on Aggregate Functions:

COUNT() and COUNT(fieldName)

Returns the number of rows matching the query criteria. For example using COUNT():

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

For example using COUNT(fieldName):

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

The COUNT(fieldName) syntax is available in API version 18.0 and later. If you are using a GROUP BY clause, use COUNT(fieldName) instead of COUNT(). For more information, see COUNT() and COUNT(fieldName).

Note that last paragraph. Specifically:

If you are using a GROUP BY clause, use COUNT(fieldName) instead of COUNT().

That means you would add COUNT(Id) as your field, and optionally include an alias:

SELECT Parent__r.Name parentName, COUNT(Id) childCount
FROM MyObject__c GROUP BY Parent__r.Name
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.