Concepts or coding lessons of Salesforce that you can implement easily

Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal

how to do it in salesforce
If you are doing a SOQL query on accounts, you want to pull data from the account’s contacts then use downwards traversal. (Parent to Child query in Salesforce)

Simply say Access Child Object Field From Parent Object in SOQL Query.

We can say pull data from a related list.

Query parent-to-child, which are almost always one-to-many. Include these relationships using a subquery which is enclosed in parentheses, where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause. Note that for subqueries, you should specify the name of the relationship for each object as that is the plural name of the object.

Downwards traversal:


Step 1: 


For nested query, you have to use the relationship name which is plural version “Contacts” in the nested SOQL query. Find the keyword of a custom relationship from the lookup or master-detail field shown in below:


  • Go to Setup -> Customize -> Contacts -> Fields -> Account Name


Step 2: 

for(Account objAcc: [select id, Name, (select id, Name, Email from Contacts) from Account where id = '001i000000eJLL4']){
   for(Contact objCont : objAcc.Contacts){
      // Access one contact fields at a time, there could be multiple results in this loop
      System.debug('Email => '+objCont.Email);
   }
}

The query returns the name and an ID Account and the Name of each contact.

Note: If you’re using custom relationships, then you have to add “__r” to the keyword: “Contacts__r”

Traverse like the parent-to-child relationship as a foreign key in an aggregate query:

SELECT Name,
  (
    SELECT CreatedBy.Name
    FROM Notes
  )

FROM Account

Above query returns all the accounts in an organization, and for each account, the name of the account, the notes for those accounts (which can be contains an empty result set if there were no notes on any accounts) with the name of the user who created that note.

If you want to add filter criteria like contact which are created-by alias "Nitish" then you can add that too:


SELECT Name,
  (
    SELECT LastName
    FROM Contacts
    WHERE CreatedBy.Alias = 'Nitish'
) 
 FROM Account

From Visualforce page:

For example, you have a requirement to display contacts of an accounts on visualforce page, then you can do it in several ways like create controller , get an Account id, then query to an Account object shown in above sample code. and then iterate contact list and store it in List of contact and return to visualforce page and display contact list using apex tags.

Other way which is best way to display Accounts related Contact on visualforce page. 
Below is the code snippet which is the best way. Salesforce handles and process it in backend and display related Contact.

Step : Create Visualforce page

<apex:pageBlock>
  <apex:pageBlockTable value="{!Account.Contacts}" var="objCon" >
    <apex:column value="{!objCon.Name}"/>
    <apex:column value="{!objCon.Id}"/>
    <apex:column value="{!objCon.Title}"/>
    <apex:column value="{!objCon.Birthdate}"/>
    <apex:column value="{!objCon.Email}"/>
    <apex:column value="{!objCon.MobilePhone}"/>

  </apex:pageBlockTable>
</apex:pageBlock>

Have I missed anything? Please comment below and I will add that to this list.


Enjoy! If you have any questions, comments, please feel free to let me know. 

As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next post: How Learning Enable Inline Editing In Visual Force Pages Could Save Your Money And Time
loading...

No comments:

Post a Comment