function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DbjensenDbjensen 

SQOL: Only return results if there is a child record

Hello - How can I prevent this query from returning results if there is no child lead?  In other words, I just want the qurey to return the contact if it has a child lead record. 
User-added image
 
for(Contact contactQueryList : [SELECT ID, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds]){
            contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
             System.debug('contact found '+contactQueryList);
                updateContactList.add(contactQueryList);
        }
Thanks for any help you can provide. 
 
Best Answer chosen by Dbjensen
Maharajan CMaharajan C
Hi Dbjensen,

Try any one of the below ways:

1. use the if condition to check the child size:
for(Contact contactQueryList : [SELECT ID, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds]){
	if(contactQueryList.Leads__r.size() > 0){
		contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
		System.debug('contact found '+contactQueryList);
		updateContactList.add(contactQueryList);
	}
}

2. Use the child soql query in parent where condition. I have considered your parent lookup field as contact__c  from so use the correct api name from lead object.
for(Contact contactQueryList : [SELECT ID, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds and ID IN: (Select contact__c from Leads__r)]){
	contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
	System.debug('contact found '+contactQueryList);
	updateContactList.add(contactQueryList);
}
https://salesforce.stackexchange.com/questions/152519/soql-to-determine-if-parent-has-child-records


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Dbjensen,

Try any one of the below ways:

1. use the if condition to check the child size:
for(Contact contactQueryList : [SELECT ID, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds]){
	if(contactQueryList.Leads__r.size() > 0){
		contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
		System.debug('contact found '+contactQueryList);
		updateContactList.add(contactQueryList);
	}
}

2. Use the child soql query in parent where condition. I have considered your parent lookup field as contact__c  from so use the correct api name from lead object.
for(Contact contactQueryList : [SELECT ID, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds and ID IN: (Select contact__c from Leads__r)]){
	contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
	System.debug('contact found '+contactQueryList);
	updateContactList.add(contactQueryList);
}
https://salesforce.stackexchange.com/questions/152519/soql-to-determine-if-parent-has-child-records


Thanks,
Maharajan.C
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Dbjensen,

You can take reference from this below code.
for(Contact contactQueryList : [SELECT ID,Contact_Sync_to_Marketing_Cloud__c, (SELECT Id FROM Leads__r) FROM Contact WHERE ID IN :contactsIds]){
    if(contactQueryList.Leads__r.size()>0){
        contactQueryList.Contact_Sync_to_Marketing_Cloud__c = true;
        System.debug('contact found '+contactQueryList);
        updateContactList.add(contactQueryList);
    }
    
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 
DbjensenDbjensen
Hi Maharajan - This works. Thanks so much!
DbjensenDbjensen
Hi Suraj - thanks for the helpful response.