+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

How do I merge the result of two different queries for the same contacts

SOQL statements cannot query more than 20 different child types.

So how do I merge the Contacts from two different queries? 

For eg. 
List<Contact> querychilds1 = [select id,Orphan__c ,
                                     (Select id from PrimaryContact__r ),
                                      (Select id from Attachments ),
                                      (Select id from CampaignMembers ),
                                      (Select id from Cases ),
                                      (Select id from CaseContactRoles ) 
                                         and few more totaling to twenty....
                                      from contact 
                                      WHERE id IN :listofcontacts]; 

          List<Contact> querychilds2 = [select id,Orphan__c ,
                                      (Select id from Feeds ),
                                      (Select id from Histories ),
                                      (Select id from DeclinedEventRelations ),
                                      (Select id from DuplicateRecordItems ),
                                      (Select id from EmailMessageRelations ),
                                      (Select id from FeedSubscriptionsForEntity ) 
                                      from contact 
                                      WHERE id IN :listofcontacts];


       
Raj VakatiRaj Vakati
You mean meging the two LIST values in one or salesforce database merge ?? 


If its list you can able to do it as below 
 
List<Contact> querychilds1 = [select id,Orphan__c ,
                                     (Select id from PrimaryContact__r ),
                                      (Select id from Attachments ),
                                      (Select id from CampaignMembers ),
                                      (Select id from Cases ),
                                      (Select id from CaseContactRoles ) 
                                         and few more totaling to twenty....
                                      from contact 
                                      WHERE id IN :listofcontacts]; 

          List<Contact> querychilds2 = [select id,Orphan__c ,
                                      (Select id from Feeds ),
                                      (Select id from Histories ),
                                      (Select id from DeclinedEventRelations ),
                                      (Select id from DuplicateRecordItems ),
                                      (Select id from EmailMessageRelations ),
                                      (Select id from FeedSubscriptionsForEntity ) 
                                      from contact 
                                      WHERE id IN :listofcontacts];
									  
									  
									  querychilds1.addALL(querychilds2);

 
SANKET KUMAR 28SANKET KUMAR 28
The above suggestion will result you with a list of contacts with duplicates, the same contacts with first 10 and then again with another set of 10 child.  You can use below solution to avoid that:-

Map<id,Contact> mapidcon1= new Map<id,Account>([select id,Orphan__c ,
                                     (Select id from PrimaryContact__r ),
                                      (Select id from Attachments ),
                                      (Select id from CampaignMembers ),
                                      (Select id from Cases ),
                                      (Select id from CaseContactRoles )
                                         and few more totaling to twenty....
                                      from contact
                                      WHERE id IN :listofcontacts]);
Map<id,Contact> mapidcon2= new Map<id,Account>([select id,Orphan__c ,
                                      (Select id from Feeds ),
                                      (Select id from Histories ),
                                      (Select id from DeclinedEventRelations ),
                                      (Select id from DuplicateRecordItems ),
                                      (Select id from EmailMessageRelations ),
                                      (Select id from FeedSubscriptionsForEntity ) 
                                      from contact 
                                      WHERE id IN :listofcontacts]);


execute you logic as following:-
for(Contact c:[select id,Orphan__c from Conatct WHERE id IN :listofcontacts]){
//execute your query
         system.debug(mapidcon1.get(c.id).PrimaryContact__r );  // this will hold all the primary contact related to particular contact
         system.debug(mapidcon1.get(c.id).Feeds );  // this will hold all the feeds related to particular contact
}