+ Start a Discussion
ChichoChicho 

count query lookup

I have an custome Invoice object with a lookup relationship to Accounts.

I'm trying to query the data base to get the total number of invoices of the accounts where Connection_Date__c has a value (Connection_Date__c is a custom field of Accounts object)

How can I do this? The query I'm using gives me only the number of accounts but not the number of invoices.

SELECT Name,(SELECT name FROM Invoices__r) FROM Account WHERE Connection_Date__c != null
ShaTShaT
Hi,

You need to loop the Account list to get the invoices.

Eg = list<Account> Acclist=[SELECT Name,(SELECT name FROM Invoices__r) FROM Account WHERE Connection_Date__c != null];
for(Account acc:Acclist){
     interger incSize = acc.Invoices__r.size();
}

incSize will give you size for invoices.

Thanks
Shailu