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
reuel3reuel3 

Issue with subquery

Here's my query that works in workbench when run with just soql. I lose the subquery data when I try to add the Contact to a list. ContactHolidayAssociations is a junction object between Holiday and Contact.

contacts = [SELECT firstName, lastName, Id, Account.Name, Account.Id, 						(SELECT Name, Holiday__r.Id, Holiday__r.Holiday_Name__c					
FROM ContactHolidayAssociations__r)
FROM Contact]

So when I do a System.debug with the contacts list, no Holiday data shows. Holiday data does show when running the same query in workbench. 

What am I missing?

 

Thanks!

GlynAGlynA

Doublecheck this please, by trying the following code:

 

contacts = [SELECT firstName, lastName, Id, Account.Name, Account.Id, 						(SELECT Name, Holiday__r.Id, Holiday__r.Holiday_Name__c					
FROM ContactHolidayAssociations__r)						
FROM Contact];

for ( Contact theContact : contacts )
{
    System.debug( 'Contact: ' + theContact );
    for ( ContactHolidayAssociation__c theContactHoliday : theContact.ContactHolidayAssociations__r )
    {
        System.debug( 'ContactHolidayAssociation: ' + theContactHoliday.Name + ' - ' + theContactHoliday.Holiday__r );
    }
}

When you convert an sObject to a String, you don't get the child objects that are contained within; but they're still there even though they don't show.  This code should reveal them.  Let me know if this works.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator