Concepts or coding lessons of Salesforce that you can implement easily

Simple Guidance For You In Access Of Subquery Field Value Using Apex - Upwards Traversal.

how to do it in salesforce
If you are doing a SOQL query on contact, you want to pull data from the account then use Upwards traversal.

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


Upwards traversal :



It means traversing from the child to the parent.

Sample Apex Code:

for(Contact tempObjCont: [SELECT Id, Account.Name, Account.status FROM Contact WHERE Name = 'Nitish'])
{
  System.debug('Account Name => '+tempObjCont.Account.Name);
}


You can traverse multiple levels upwards !!!

Below is the SOQL where I fetched the Account`s owner`s Profile creator name in Contact object.

SELECT Account.Owner.Profile.CreatedBy.Name FROM Contact

Note: If you are using custom lookup field then use "__r" when traversing that field.


Account.Name, AccountId . is the field representation used in soql only in case of standard relationships, 
for eg. you can use Select Account.name from contact or select Account.Name from Opportunity etc. where Account, Contact, Opportunity were standard objects and relationship between them is the Out of box relationship.

In case of  custom relationships, '__r' should be appended in order to access the parent records field information via soql, for eg., let us assume Account__c is api name for the Master Detail field on any Standard or Custom object, in that case:


Select Account__r.Name from Object_API_Name is the syntax that needs to be used to fetch details from Account.

Sample Apex code: 

List<Account> accounts = [SELECT Id, Name, (SELECT Id, XYZ__r.Id, XYZ__r.Name FROM ABC__r) FROM Account];

for (Account acct : accounts) {
    for(ABC__c objABC : acct.ABC__r)
       {
          String testValue = objABC.XYZ__r.Name;
       }
}

Here, ABC__r is the relationship of object ABC_c with Account. 

More Salesforce Blogs:

Check out Salesforce Daily Limit Only in 5 Simplest Steps
Schedulable Batch Apex In 3 Easy steps In Salesforce
Learning Pagination In Salesforce Is Not Difficult At All ! You Just Need 3 Easy Steps
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Main Difference Between ISBLANK And ISNULL in Salesforce
How To Get Total Amount Of Records Processed In Batch Job In 10 Minutes And Still Look Your Best 
Export VisualForce Data into Excel Sheet in 3 Easiest Steps
7 Easy Steps to Generate Apex Class From WSDL In Salesforce
Simplest Way To Find Number of Days Between Two Dates in Salesforce
3 Easy Steps To Send Emails With Attachment From Your Apex Class In Salesforce
How Insert Comma In Formula Fields Can Help You Improve Your Productivity
Simple Guidance For You In Access Of Subquery Field Value Using Apex - Upwards Traversal.
Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal
How Learning Enable Inline Editing In Visual Force Pages Could Save Your Money And Time


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: Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal
loading...

2 comments: