+ Start a Discussion
sharath kanukuntla 7sharath kanukuntla 7 

How to get the AccountId Whose Contacts are more than 10.

Best Answer chosen by sharath kanukuntla 7
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

Please use below query to get the Account which has more than 10 contacts:

 

SELECT AccountId, COUNT(Id)
FROM Contact
GROUP BY AccountId
HAVING COUNT(Id) > 10

All Answers

pconpcon
Can you expand on what you are asking?  More than 10 what?  You want accounts that have more than 10 contacts?  Are you trying to do this in Apex?  Please let us know what your end goal is and we can help you reach it.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

Please use below query to get the Account which has more than 10 contacts:

 

SELECT AccountId, COUNT(Id)
FROM Contact
GROUP BY AccountId
HAVING COUNT(Id) > 10

This was selected as the best answer
Rakesh Thota 15Rakesh Thota 15
Hi Sharath kanukuntla 7,

To achieve this need to work with SOQL Aggregate Functions, Here is the sample code for your requirement.

Apex code:
public  class Accts_ids {
    
    public AggregateResult[] results { get {
    return [SELECT accountid idsss, count(id) contactssss FROM Contact GROUP BY          Accountid HAVING count(id) > 10];
        }
    }
}
Visualforce page code:
<apex:page controller="MyController">
    <apex:pageBlock title="AccountID's which associated with more than 5 contacts">
        <apex:pageBlockTable value="{!results}" var="r">       
          <apex:column value="{!r['idsss']}"/>                
          <apex:column value="{!r['contactssss']}"/>        
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

please refer this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

Hope this helps you!

Best Regards,
Rakesh Thota.


 
sharath kanukuntla 7sharath kanukuntla 7
Thank You, Deepak Maheshwari.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

If my solution works for you.

Please mark it as the best answer.

 

Thanks
Deepak

sharath kanukuntla 7sharath kanukuntla 7
Thank you, Rakesh Thota. Its Working.