1

By using inner query its working but the question is soql work on 50,000 records only if one Account has suppose 60,000 Contact(child) records then it'll not work. so how i can count no. of child

Parent is Account Child is Contact

if(AccountIds != null){

            List<Account> accList = [SELECT id,AccountContacts__c,(SELECT Id From Contacts) From Account WHERE ID IN :AccountIds ];
            for(Account acc : accList){
                acc.AccountContacts__c = acc.Contacts.size();
            }
            if(accList != null){
                update accList;
            }

        }
4
1

I am improving my answer from the previous one completely.

  • If parent-child is master-detail, use roll-up summary.
  • If parent-child is lookup and within 50k soql range, use trigger to process. (Tell me if you need code for the same)
  • If parent-child is loop and more than 50k soql range, use aggregate query like this [select accountid,count(accountid) from contact group by accountid] - tested
2
  • While normally a good suggestion, this is not possible in this case. Contact is, for some unknown but still unfortunate reason, not an object that can be the target of a rollup summary field on Account.
    – Derek F
    Aug 25 '17 at 14:59
  • Do not solicit accept votes in your answers.
    – Adrian Larson
    May 16 '19 at 17:43
-1

NEW ANSWER

The use case you're describing is an unusual one and you may want to rethink it. Will you really have Accounts with over 50,000 children? What do you need the children count for in the first place?

If you don't need to further manipulate the count, the best solution may simply be to retrieve this via reporting.

EDIT: The below is the original answer, but it is inaccurate. You cannot have a master-detail relationship on Contact to Account due to inherent platform limitations

If you really do have this use case, you may have better luck setting up a rollup summary field on the Account to count all the contacts. In order to do this you'll need to create a Master-Detail Account relationship on the Contact and keep this in sync with the standard Account lookup, but this can be done without too much trouble using Apex or Workflows.

Note that this will only work if your use case does not allow for account-less contacts.

4
  • 1
    I could be mistaken, but I don't believe that you can add a new Master-Detail relationship on any standard object in Salesforce (an M-D relationship is possible between a standard and custom object, but what you're suggesting would be an M-D relationship between two standard objects). Contact already has a relationship to Account, but unfortunately, Contact is not available in the list of objects that can be summarized when you try to create a rollup-summary field on Account.
    – Derek F
    Aug 25 '17 at 15:02
  • You're absolutely correct, I hadn't realized this was the case. If this is the case, the only thing I can really think of for this use case is to accomplish it with reporting. Is the proper etiquette here to edit my original answer since it contains inaccurate information?
    – Ginour
    Aug 25 '17 at 21:39
  • 1
    Etiquette here is mostly community-defined, so it's not really written down (that I know of) outside of the basics. Since comments are technically temporary though, I would probably make an edit along the lines of "...or at least that's how you would do it...but in this case Salesforce blah blah blah" if it were one of my own answers.
    – Derek F
    Aug 25 '17 at 21:49
  • @Ginour yes I have the scenario that has more then 50,000 child. Their are multiple companies Like Google, IBM, Cognizant and they have more than 50,000 employee Across the world and requirement is showing No. of Employee (Contact) for that Requirement. Aug 29 '17 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.