4

So this would be possible with a roll-up summary, but since there's a limit on the number of these available, I'm trying to accomplish it without the roll-up summary field.

Via SOQL I need to get a list of parent records (for example Accounts) which have 0 children (for example Contacts).

Ideally it'd be something along these lines, but alas no luck:

select Id
from Account
where (select count() from Contacts) = 0

Ideas?

4

You can check for parent Id of the child records to get this.

SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact)
2
  • This answer was posted about 10 minutes after I posted my answer...
    – Ray Dehler
    Jul 18 '14 at 21:04
  • This is normally happening when people helping the community in parallel with different questions. While replying to you some others putting comments for theirs one. After looking at them and come back, hit enter for yours some similar answers can be there..
    – highfive
    Jul 19 '14 at 17:44
2

Found it!

select Id
from Account
where Id not in (select AccountId from Contact)

Thanks!

2
  • 1
    What happens when there are a large # of contacts (lets say a million). Won't this query hit limits
    – NSjonas
    Mar 29 '16 at 22:14
  • I agree with you @NSjonas because it will fail when there will more than 50k of contacts in database. May 29 '18 at 23:04

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.