+ Start a Discussion
smitrasmitra 

Retrieving Related List by SOQL query and disply in a visualforce page

I have two custom objects as   master detail relationship.Inorder to retrieve related list from the master record using SOQL query and display it in visualforce page

Best Answer chosen by Admin (Salesforce Developers) 
sandeep@Salesforcesandeep@Salesforce

Here "detail__rr" is child relation ship name between child and master object. you can get child relation ship name from Master detail field on child object's schema. there child relation ship is available and then please append "__r"  

All Answers

Rahul_sgRahul_sg

You can use parent object ID to query related childs in SOQL or you can use 

apex:relatedList 

sandeep@Salesforcesandeep@Salesforce

Let's say I have two objects Master and Detail ( Child object)  ten in order to display related list of child record we need to apply SOQL like below 

 

List<Master__c> ListOFMasterRecords = new list<Master__c>([select id (select id from Detail__r)from Master__c]);

 

 

 

 

Yoganand GadekarYoganand Gadekar

you can fire relationship query which will give you all the detail(related list records) records for each of the master record.

 

Have a look at this : How to fetch related list records using Relationship query in apex.

 

Hit kudos and mark this as answer if this helps you in any way.

 

Thanks,

smitrasmitra
What detail__r implies?
Avidev9Avidev9

Lets Elaborate the Example Using Account and Contact.

 

  • Parent Object : Account
  • Child Object : Contact
  • Child Relationship Name : This is very important and is required for 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 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 statement. If it was a custom object you will have to add "__r"

So your query will be

 

List<Account> acc = [SELECT Id,Name,(SELECT Id,LastName FROM Contacts) FROM Account];

 How to access ?

 

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

 

sandeep@Salesforcesandeep@Salesforce

Here "detail__rr" is child relation ship name between child and master object. you can get child relation ship name from Master detail field on child object's schema. there child relation ship is available and then please append "__r"  

This was selected as the best answer
smitrasmitra

Hi thanks all for your response.

 

Now can you tell me incase if I need to sort the related list that I have fetched what is the solution for that.

 

what best suits with this code snippents inorder to sort related list

 

<apex:pageBlockTable value="{!relatedContacts}" var="val">
<apex:column value="{!val.name}"/>
<apex:column value="{!val.email}"/>
</apex:pageBlockTable>

 

sandeep@Salesforcesandeep@Salesforce

Thanks for marking my answer please hit STAR icon to give me KUDOS