Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

Hello Folks,

I am working on customizing some functionality on the Force.com platform. I have a question for you; I am not too sure if this is possible!

What I have? : 2 custom objects - abc and pqr. Abc is the junction object between the Standard object Account and pqr i.e. Account is the parent of abc and then Pqr is the parent of abc. I am retrieving everything through these objects into a List of type Account (i.e. from Account object).

What do I need? : I can access the abc object in Apex i.e. the First child of Account. Is there a possibility to access the fields of pqr through account?

What I have tried : Accessing the object through relationship name - Account.abc_r[0].pqr_r[0].FIELDNAME

But it hasnt worked. Could some salesforce/apex developer help me with this?

1 Answer

0 votes
by (32.1k points)

You must get the information you need using a subquery. The real question here is how your query is setup. Your query will need to access all levels of the data model for it to be available. If you are relying on data returned in a trigger or standard controller then I would recommend requiring the account object to get access to the additional information.

So I would expect to see something along the lines of:

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

for (Account acct : accounts) {

    string someValue = acct.abc__r[0].pqr__r.Name;

}

Keep in mind though that as a best practice you shouldn't access the child records the way I did above because if there are no ABC records that object will be null so as a best practice you will want to test for that ahead of time. Same goes for PQR object data.

Be a certified Salesforce professional by going for Intellipaat’s Salesforce Certification!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...