You need to sign in to do that
Don't have an account?
Bryan Hunt
SOQL query, WHERE clause reference to related list size
I have seen threads with dozens of questions about this over the last 6-7 years, all with the answer of "can't be done". But, I keep hoping that each new version of SFDC might include a way of accomplishing it, so I'll ask the question once again.
I would like to use SOQL to query a parent object based on whether it does or does not have any child records (whether MD or Lookup).
The perfect query would be something like:
- SELECT Id FROM Account WHERE COUNT(Contacts__r) = 0
or
- SELECT Id FROM Account WHERE Contacts__r.size() = 0
or
- SELECT Id FROM Account WHERE Contacts__r = null
From what I have found in threads as late as 2013, this does not appear to be possible.
Is that still the case in the Summer 2014 release?
It would appear that (for my needs) the best way to execute this type of query would be to:
- SELECT Name, (SELECT name FROM Contacts) FROM Account
- Then programmatically check the size of the returned set of Contacts
Is this my best option? I know that I could:
- SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact)
But the actual objects that I want to interogate are more that the 50k limit for this type of query.
Thanks.
Bryan Hunt
I would like to use SOQL to query a parent object based on whether it does or does not have any child records (whether MD or Lookup).
The perfect query would be something like:
- SELECT Id FROM Account WHERE COUNT(Contacts__r) = 0
or
- SELECT Id FROM Account WHERE Contacts__r.size() = 0
or
- SELECT Id FROM Account WHERE Contacts__r = null
From what I have found in threads as late as 2013, this does not appear to be possible.
Is that still the case in the Summer 2014 release?
It would appear that (for my needs) the best way to execute this type of query would be to:
- SELECT Name, (SELECT name FROM Contacts) FROM Account
- Then programmatically check the size of the returned set of Contacts
Is this my best option? I know that I could:
- SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact)
But the actual objects that I want to interogate are more that the 50k limit for this type of query.
Thanks.
Bryan Hunt
I would suggest you to store the Contacts count in a custom field on Accounts. This update can be done using a trigger. You can then use this field in your WHERE clause.
Thanks,
Shashank
Is it mandatory to use SOQL in a way you suggested? Is it possible to use aggregate queries? Like this :
Will that work? Else am afraid the answer is still the same :(
Ankit, in this case we will be reading 300k+ parent records so we may need to just use the programmatical check to see if there are >0 child records.
I wonder how Salesforce does this? When you pull up an Account record, the counts for every related object pop up immediately (in our case, 12 related objects). I can hardly believe that they do 12 'select count()' queries just to bring up the single Account record?
Thanks.
Bryan Hunt
Check it out.
list<Account> accounts = [SELECT Id FROM Account WHERE id NOT IN (select AccountId from Contact)];
It returns only accounts with no contacts.