Apex - SalesForce - access to the field of the parent object through the Junction object

Hello people,

I am working on setting up some features on the Force.com platform. I have a question for you; I'm not sure if this is possible!

What is mine? : 2 user objects - abc and pqr. Abc is the connection object between the standard object account and pqr ie The account is the parent of abc, and then Pqr is the parent of abc. I retrieve everything through these objects to the List of type account (i.e. From the Account object).

What I need? . I can access the abc object in Apex, that is, the first child of the account. Is it possible to access pqr fields through an account?

What I tried : access to the object through the name of the relationship - Account.abc_r [0] .pqr_r [0] .FIELDNAME

But he did not work. Can a Salesforce / apex developer help me with this?

+3
source share
2 answers

You should be able to obtain the necessary information using a subquery. The real question here is how your request is configured. Your query will need access to all levels of the data model in order to be accessible. If you rely on data returned by a trigger or a standard controller, I would recommend that the account object gain access to additional information.

So, I expected to see something like:

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;
}

, , , , ABC, , , , . PQR.

+4

:

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

for (Account acct : accounts) {
    for(Abc__c abc : acct.abc__r)
       {
          String someValue = abc.pqr__r.Name;
       }
}

. abc_r - Abc_c . , - .

+2

All Articles