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
Arav SundarArav Sundar 

How to query the count of child record

Hi ,
i am having an custom object of Location which has a lookup to account and i am trying to perform the soql query to get the locations which is greater than 1 from the account object , below is the SOQL query can someone see this and suggest me on the same

Select id,name,(SELECT id,Name From SVMXC__Site__r ) from Account
Amit Chaudhary 8Amit Chaudhary 8
I tested same query with contact in my dev org like below
List<Account> lstAccount = [ Select id,name,(SELECT id,FirstName From Contacts ) from Account ]; 

For ( Account acc : lstAccount ) 
{ 
	List<Contact> lstCont = acc.Contacts; 
	if( lstCont .size() > 1 ) 
	{ 
		System.debug('More then one Contact'+lstCont ); 
	} 
}



TRy below code.
 
List<Account> lstAccount = [ Select id,name,(SELECT id,Name From SVMXC__Site__r ) from Account ];
For ( Account acc : lstAccount )
{
     List<SVMXC__Site__c> lstSite = acc.SVMXC__Site__r;
     if( lstSite.size() > 1 )
     {
        System.debug('More then one site');
     }
}

Let us know if this will help you
Arav SundarArav Sundar
Hi Amit ,

this is the code my thing is i just want to query the count of the related list in SOQL Query itself
Amit Chaudhary 8Amit Chaudhary 8
I dnt think so you can do the same in SOQL itself. As you can not perform the count in inner query

User-added image

You can try below code.
List<Account> lstAccount = [ Select id,name,(SELECT id,FirstName From Contacts ) from Account ]; 

For ( Account acc : lstAccount ) 
{ 
	List<Contact> lstCont = acc.Contacts; 
	if( lstCont .size() > 1 ) 
	{ 
		System.debug('More then one Contact'+lstCont ); 
	} 
}

PLease let us know if this will help you
Arav SundarArav Sundar
Yes i also found that we cannot perform this in SOQL query , thanks for your help Amit