Relationship Query use - apex in salesforce

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10

Sometimes we need list of all the child records of a parent record. For example we may need all the contacts of a account.

These records can be retrieved using relationship query. You can say it to be a query within a query. Before we write a relationship query,

We need to know more about lookup field. every look up field will have a child relationship named assigned to it. we can change this name as desired.

This childrelationship name is used in relationship query to refer to the child object,  so we need to find out this name before we write the query. You can find this just below the api name of the field.

Following screen shot shows the account look field details(you can see child relationship name as "contacts"). 
  
Standard look up's given by salesforce will have fixed child relationship names and cannot be changed.

Custom fields created by user can have CR names as desired by user.

Lets try to get get all the contacts of account in below example:
 * overide a custom button on account with this vf page

 -- VisualForce Page --

<apex:page standardController="Account" extensions="contrller">
  <apex:form >
    <apex:pageblock >
       <!-- Display Account related Contacts -->
       <apex:pageBlockTable value="{!relatedContacts}" var="val">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.email}"/>
       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>


 -- Controller --

public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;
    }

    public List<Contact> getrelatedContacts(){
        List <contact> conList = New List<Contact>();
        for(Account acc:[select id,name,(select name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               conList.add(con); 
        }
        return conList;
    }
}

Note that inner query contact "select name,id from contacts" has childrelationship name as mentioned above.

Suppose if you have a custom object lookup and you want to retrieve the these records under account then you have to follow similar steps as above but make sure you append __r to your child relationship name.

If you have a child object AccountChild__c of account and you want to retrieve this objects records then just change the method as below:

Your field name on AccountChild__c for account look is "AccountChild__c" and child relationship name is "AccountChilds"


-- VisualForce Page --

<apex:page standardController="Account" extensions="contrller">
  <apex:form >
    <apex:pageblock >
       <!-- Display custom object records related to account  -->
       <apex:pageBlockTable value="{!relatedCustObjRecs}" var="val">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.Id}"/>
       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>


-- Controller --
 
public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;
    }

     public List<AccountChild__c> getrelatedCustObjRecs(){
       List<AccountChild__c> conList = New List<AccountChild__c>();
       for(Account acc:[select id,name,(select name,id from AccountChilds__r) from account where id=:Current_Acc_Id]){
           for(AccountChild__c con:acc.AccountChilds__r)
               conList.add(con); 
       }
       return conList;
    }

}

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10

9 comments:

  1. Very good Explanation Yoganand. Easy to understand.

    ReplyDelete
  2. What I found was pretty cool Was to create a list of contact with an option for a checkbox. Then create accounts with select option(drop down). If contact is select relate it to an account when saved.

    Best regrads

    ReplyDelete
  3. Is there any way to use the relatedLists instead of the pageBlockTable?

    ReplyDelete
  4. Is there any way to use relatedLists instead of apex:pageBlockTable?

    ReplyDelete
  5. For details information , go through Salesforce Docs https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

    ReplyDelete
  6. https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

    ReplyDelete