+ Start a Discussion
Soumya Srivastava 1Soumya Srivastava 1 

Not getting data from dynamic SOQL through Database.Query to get Related List data

Hi,

I am not getting data through dynamic soql in apex through database.query.
              
                  String query = 'select (select ' + relatedFields + ' from ' + s +' ) from Contact Where ID = \'0036F000022zMpg\'';  

Whenever, I am accessing relatedList fields to access data it is returning me Contact:{Id=0036F000022zMpgQAE}} in this format which is the key. Please let me know if any of you have any idea to access related list data from any sObject through Dynamic Soql.

Thanks,
Soumya      
                  
        
SandhyaSandhya (Salesforce Developers) 
Hi,

Please check the if you have given the right child relationship name.

 You can use https://workbench.developerforce.com tool to get child relation name.

Go to: Info -> standard & custom obj (choose your object) -> Child relationship -> relationshipName

For Example

Parent Object: Account
Child Object: Contact
Child Relationship Name: This is very important and is required for a parent to child queries. You can find the same in the relationship field that relates the child object to account. In our case, it is "Account" which is located @Contact. So if you go to the detail of this field, you will find the Child Relationship Name, which should be "Contacts." So the child to parent relationship will have this in its from the statement. If it were a custom object, you would have to add "__r"
So your query will be
 
List<Account> acc = [SELECT Id,Name,(SELECT Id,LastName FROM Contacts) FROM Account];

To access

for(Account a : acc){
   List<Contact> contactList = a.Contacts;
}

 
Please refer below link for reference.

http://salesforce.stackexchange.com/questions/51980/how-to-retrieve-the-related-list-data
 
Hope this helps you!

If this helps you, please mark it as solved.

Thanks and Regards
Sandhya
Soumya Srivastava 1Soumya Srivastava 1
Hi Sandhya,

Thanks for your reply, i understand about the relationships and already got those. Now the problem is getting its fields in a map dynamically and showing it on a Vf page.I am able to get data based on relationship in the debug logs. Now, I just need to bind map dynamically.
Here is the snippet of the code :
<apex:repeat value="{!mapOfRelatedListFields}" var="key">  
                <apex:outputText value="{!key}"/> <br/>
                <apex:repeat value="{!mapRelatedList[key]}" var="dd">
                    <apex:outputText value="{!dd}"/> <br/>
                </apex:repeat>
           </apex:repeat>
        
// Apex snippet
          for(String s:mapOfRelatedListFields.keySet()){
              
                  String relatedFields = string.ValueOf(mapOfRelatedListFields.get(s)).removeStart('(').removeEnd(')');
                  
                  System.debug('relatedFields====='+relatedFields);

                  
                  commaSepratedFields = commaSepratedFields +', (select  ' + relatedFields + ' from ' + s +' )'; 
                  strList.add(s);
                  
         
              } 
            
            
           /* -----------  making the query to execute ---------- */
            query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where ID = \''+ String.escapeSingleQuotes(obj)+'\'';   
           System.debug(query);
    
           recDetail = Database.query(query);
           system.debug('The record details are:-'+recDetail);
           
           mapRelatedList = new Map<String, List<sObject>>();
             
            for(String str: strList){
            
                if(recDetail.getSObjects(str) == null){
                    mapRelatedList.put(str, new List<sObject>());
                }
                else{
                    mapRelatedList.put(str, recDetail.getSObjects(str));
                }
               
            }

            //System.debug('mapRelatedList====' +mapRelatedList);

Thanks,
Soumya