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
Simon Coles 2Simon Coles 2 

Multilevel SOQL Query (Parent to Children) more than 1 level

I am trying to get some related contact information from a SOQL query but have run into the error "SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object."

Here is my relationships; Activity_List__c <-- Activity_List_Contacts__c --> Contact (Junction Object)

Activities can have Sub Activities (Only one Level) Multiple contacts can be assigned to each activity.

Here is my sample SOQL query :

select id, Name, (select id, Name, (SELECT Id, Contact_Name__r.FirstName, Contact_Name__r.LastName FROM Activity_List_Contacts__r) from Activity_Lists__r limit 50000), (SELECT Id, Contact_Name__r.FirstName, Contact_Name__r.LastName FROM Activity_List_Contacts__r) from Activity_List__c where Campaign__c ='701M0000000O7Ic' limit 50000

I know I need to use a map from reading the boards but am not sure how / what I need to do to accomplish it.

Thanks Colsie
Sanjay.GeorgeSanjay.George
Hi Colsie,

I would ask ask you to start analysing how you want to display the information.

if you just want to view all contacts and activity under a given campaign here it goes:

select id, activity_list__r.<activity fields>, contact.<contact fields> from activty_list_contact__r where activity_list__r.campaign='<campaigned>'

This will return all the activity list and contacts associated to the campaigns. You can then create a map of activity list to list<contacts> to list them out.