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

16 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
  7. The innocent party will feel quite betrayed and actually start to resent you for not trusting them in the first place. So either way your relationship is in big trouble lie detector uk

    ReplyDelete
  8. A good relationship is not yours of right, but you can have a good relationship if you are prepared to work for it. things to talk to girls about

    ReplyDelete
  9. Have you heard of the saying that says that "what goes for the duck is also good for the geese" meaning that somebody somewhere is also having a serious challenge in his or her own relationship.Atlanta escorts

    ReplyDelete
  10. Albeit nobody can count every one of the reasons why relationships fizzle, we have recorded here the best reasons. So what are these relationship executioners? hidden profiles

    ReplyDelete
  11. Need some Fun Date Ideas? You've come to the right place. A lot of the fun dating ideas listed below are not recommended as a first date unless you know the person very well or have known them for a long time. คลิปหลุด

    ReplyDelete
  12. Contingent upon what we realized in our initial years, we could possibly need to show these relationships. Recuperating these first relationships altogether influences the soundness of our relationships today.Virginia pesonals

    ReplyDelete
  13. You can't avoid being you and the person being referred to is what the individual is. Nuances and eccentricities are a bit of our peculiarity and differences.the hero instinct

    ReplyDelete