+ Start a Discussion
Hema YejjuHema Yejju 

A non foreign key field cannot be referenced in a path expression: Case_Exts__r at line 1,686 column 23

I have one query and when im trying to iterate over it was showing the error.
[SELECT AccountSource,Account_Status__pc,Address_Verification_Status__pc,Agency_Code__c,Agency_Display_Name__c ,(
                                    SELECT Applicant__c,Appraisal_Value__c,Area__c,Account__c FROM Case) FROM Account WHERE Id IN: caseIdList];

I have this query while im writing like this :

                for(Account cs:acclist){
                                    
                                    if(cs.Case__r.Area__c!=null){
                    
                    htmlBody += '<tr><td style="border:1px solid red">' + 'CaseNumber' + '</td><td  style="border:1px solid ">' + cs.CaseNumber + '</td></tr>';
                }
Best Answer chosen by Hema Yejju
ravi soniravi soni
hi Hema,
Your Query is looking worng. if you want to get Account  child record Case then you have to use Cases. and if you are using Custom object then you need to try Case__r.
Below code will help you for better understanding.
for(Account acc: [SELECT Id,Name,(SELECT Id,casenumber FROM Cases) From Account limit 1]){
    for(case oCase : acc.Cases){
        system.debug(oCase.casenumber);
    }
}

take reference from above code.
don't forget to mark it as best answer.
Thank you
 

All Answers

ravi soniravi soni
hi Hema,
Your Query is looking worng. if you want to get Account  child record Case then you have to use Cases. and if you are using Custom object then you need to try Case__r.
Below code will help you for better understanding.
for(Account acc: [SELECT Id,Name,(SELECT Id,casenumber FROM Cases) From Account limit 1]){
    for(case oCase : acc.Cases){
        system.debug(oCase.casenumber);
    }
}

take reference from above code.
don't forget to mark it as best answer.
Thank you
 
This was selected as the best answer
ravi soniravi soni
hi Hema,
here is your code reference.
List<Account> LstAcc = [SELECT AccountSource,Account_Status__pc,Address_Verification_Status__pc,Agency_Code__c,Agency_Display_Name__c ,(
                                    SELECT Applicant__c,Appraisal_Value__c,Area__c,Account__c FROM Cases) FROM Account WHERE Id IN: caseIdList]
for(Account acc :LstAcc ){
     
        for(Case Ocase :acc.Cases){
              /*if(Ocase.Area__c!=null){
              //Code
              }*/
}
       
    }
try both code and let me know your status.
don't forget to mark it as best answer.
Thank you
 
ravi soniravi soni
hi Hema,
did you try above solution.
let me know by marking it as best answer.
don't forget to mark it as best answer.
Thank you