+ Start a Discussion
Sascha DeinertSascha Deinert 

Related Parent Parent Field

Hi,

at the moment I get all information for the account with the code below.
Now I need the information from a related parent parent object.

Account --> Contract --> Salesnumbers

How can I get the information from the salesnumbers object?
Public List <Account> getAccDList2() {
    List <Account> AccD =  [SELECT Id, Name, BillingCity, Rating, Billingstreet, BillingPostalCode, Folder_Link__c, NumberOfEmployees FROM Account WHERE Id = :SelectedAccountId];
    RETURN AccD;
}
Thanks,
Sascha
Amit  TrivediAmit Trivedi
Hi Sascha,

Try below query

List<Salesnumber__c> fetchSalesnumbersAcc =[Select Id,Contract__c,Account.Id,Account.Name,Account.BillingCity,Account.Rating, Account.Billingstreet,Account.BillingPostalCode,Account.Folder_Link__c,Account.NumberOfEmployees FROM Salesnumber__c WHERE Contract__r.Account.Id = :SelectedAccountId];
 make changes on child relationship name and field name.

Thanks,
Amit Trivedi
surasura
Public List <Account> getAccDList2() {
    List <Account> AccD =  [SELECT Id, Name,Contract__r.SalesNumbers__r.Name BillingCity, Rating, Billingstreet, BillingPostalCode, Folder_Link__c, NumberOfEmployees FROM Account WHERE Id = :SelectedAccountId];
    RETURN AccD;
}
you can travers up in your object structure using __r if you want to access SalesNumbers fields through contract object just use

Contract__r.Sales_Numbers__r.fieldApiName

here I assume Contract and SalesNumbers are custom objects  and there api Names are Contract__c and Sales_Numbers__c . modify you code with releavant api names


 
Rohini AherRohini Aher

Hi Sascha, 

You wont be able to query on Account to ge the fields of Salesnumber__c object as parent-child queries go upto one level below only and you want to access till second level. Try querying on Salesnumber object and fetch all account fields using "Contract__r.Account" relationship.

Thanks,
Rohini

 

Sascha DeinertSascha Deinert
Hi,

@Sura
I get an error : Didn't understand relationship 'Contract__c__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names
Public List <Account> getAccDList2() {
    List <Account> AccD =  [SELECT Contract__c__r.Name, Id, Name, BillingCity, Rating, Billingstreet, BillingPostalCode, Folder_Link__c, NumberOfEmployees, FROM Account WHERE Id = :SelectedAccountId];
    RETURN AccD;
}

@Amit
I get an error: Didn't understand relationship 'Account' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names
Public List <salesnumbers__c> getAccDListTest() {
List<salesnumbers__c> AccDTest =[Select Account.Id, Name FROM salesnumbers__c WHERE Kundenbeziehung__r.Account.Id = :SelectedAccountId];
Return AccDTest;
}

Any ideas?

Thanks,
Sascha
surasura
could you tell Contract object you are using is the Standard object or it is a custom object

if your contract is  a  parent of  Account and  if it is a custom object systax should be as below 


 contract__r.Name not Contract__c__r.Name
Sascha DeinertSascha Deinert
@Sura,

It's a customobject. The realname is Kundenbeziehung__c (german word).
I changed the syntax, but I still get the same error. The object Kundenbeziehung__c is a parent of Account (Master). 
Public List <Account> getAccDList2() {
    List <Account> AccD =  [SELECT Kundenbeziehung__r.Name, Id, Name, BillingCity, Rating, Billingstreet, BillingPostalCode, Folder_Link__c  FROM Account WHERE Id = :SelectedAccountId];
    RETURN AccD;
}
surasura
 if possible could you give me  a screenshot of the field list  of account object . (field linking Account to Kundenbeziehung will be enough)
Sascha DeinertSascha Deinert
@Sura,

at the Account object I have just rollup fields to the Kundenbeziehung object.
Account Fields

but at the Kundenbeziehung object I have a master detail field related to the Accout object.
Kundenbeziehung Field
 
surasura
I see that the issue since Account is the master of Kundenbeziehung__c  one account can have many Kundenbeziehung__c   records therefore you cant query like above . You have to create a data strucure like a Map<Account,List<Kundenbeziehung__c>>  if you want to display Kundenbeziehung__c   related to accounts.
And you have to query Kundenbeziehung__c    related to accounts seperately in a diffent SOQL (subquery is possible but bit complex)
Sascha DeinertSascha Deinert
@Sura,

It is possible to get the information with the code below?
I tried it with the code, but I didn't get a value.

Thanks,
Sascha
 
public void getUmsatz() {

    List<Kundenbeziehung__c> GRVList = [SELECT Id, Name, Vertragsnummer__c FROM Kundenbeziehung__c WHERE Unternehmens_Id__c = :SelectedAccountId];

        FOR (Kundenbeziehung__c GRV : GRVList) {

    List<Umsatz_Historie__c> UmsatzList = [SELECT Id, Name, Bewertungssumme__c FROM Umsatz_Historie__c WHERE Kundenbeziehung__c = :GRV.Vertragsnummer__c];

        FOR (Umsatz_Historie__c Umsatz : UmsatzList) {

            Umsatzvolumen += Umsatz.Bewertungssumme__c;

}

}

}

 
Udesh De SilvaUdesh De Silva
try the below 
 
public void getUmsatz() {

    List<Kundenbeziehung__c> GRVList = [SELECT Id, Name, Vertragsnummer__c FROM Kundenbeziehung__c WHERE unternehmen__c= :SelectedAccountId];

        FOR (Kundenbeziehung__c GRV : GRVList) {

    List<Umsatz_Historie__c> UmsatzList = [SELECT Id, Name, Bewertungssumme__c FROM Umsatz_Historie__c WHERE Kundenbeziehung__c = :GRV.Vertragsnummer__c];

        FOR (Umsatz_Historie__c Umsatz : UmsatzList) {

            Umsatzvolumen += Umsatz.Bewertungssumme__c;

}

}

}

i am not sure what you do with Umsatzvolumen += Umsatz.Bewertungssumme__c; line 
Sascha DeinertSascha Deinert
@Udesh,

Still not works.

Umsatzvolumen += Umsatz.Bewertungssumme__c; is to build a sum of the Bewertungssumme (values).

I checked the List Kundenbeziehung query also with a calculation and don't works too.

I posted my code below where I call the getUmsatz function. Is that correct?
Public List <Account> getAccDList2() {
    List <Account> AccD =  [SELECT Id, Name, BillingCity, qualitative_Kundenklassifizierung__c, Rating, Billingstreet, BillingPostalCode, Folder_Link__c, NumberOfEmployees, Anzahl_Einzelrisiken__c, Teilnahmequote__c  FROM Account WHERE Id = :SelectedAccountId];
    getUmsatz();
    RETURN AccD;
} 

Public pageReference getAccDList() {
  getAccDList2(); 
  RETURN NULL; 
}  

public void getUmsatz() {

    List<Kundenbeziehung__c> GRVList = [SELECT Id, Name, Vertragsnummer__c, Anzahl_Einzelrisiken__c FROM Kundenbeziehung__c WHERE Unternehmens_Id__c = :SelectedAccountId];

        FOR (Kundenbeziehung__c GRV : GRVList) {
        
            SummeEZR += GRV.Anzahl_Einzelrisiken__c;
            

    List<Umsatz_Historie__c> UmsatzList = [SELECT Id, Name, Bewertungssumme__c FROM Umsatz_Historie__c WHERE Kundenbeziehung__c = :GRV.Vertragsnummer__c];

        FOR (Umsatz_Historie__c Umsatz : UmsatzList) {

            Umsatzvolumen += Umsatz.Bewertungssumme__c;

}

}

}

Thanks,
Sascha
Sascha DeinertSascha Deinert
Any ideas?

Thanks.