2

I have the following simple query which shows I can access the field I want to filter by:

SELECT Id, Name, (SELECT HC4__IsSearchableExternally__c FROM Contacts)
FROM Account

However, what I really want to do is return only the Id and Name properties for Accounts that have at least one Contact where HC4__IsSearchableExternally__c is true. Is this possible to do with a Salesforce query?

Basically, I want to do something like the following (nonfunctional query):

SELECT Id, Name
FROM Account
WHERE (SELECT COUNT(Id) FROM Contacts WHERE HC4__IsSearchableExternally__c = true) > 0

Thanks for any help you can provide!

KyleMit
  • 30,350
  • 66
  • 462
  • 664
JKasper11
  • 772
  • 1
  • 7
  • 21

1 Answers1

4

You can do this with a semi-join, e.g:

select id, name from account 
where id in (select accountId from contact where HC4__IsSearchableExternally__c = true)
KyleMit
  • 30,350
  • 66
  • 462
  • 664
superfell
  • 18,780
  • 4
  • 59
  • 81
  • Can we query parent record( say, Account) based on 2 filter criteria based on 2 child objects (say, Contact and another object-Test)? – VISHNU SANTHOSH Apr 10 '23 at 13:21