+ Start a Discussion
Sammy7Sammy7 

invalid foreign key relationship: access field on custom object with master detail to opportunity

So I have custom object (invoice__c) with master-detail to opportunity. I have a custom field (AF_LastSyncTime__c ) on invoice__c that Im trying to access.
so here is my query:
for (Opportunity opp : [select Id, (select id, AF_LastSyncTime__c from Invoices__r),
                       (select Id, Invoice_No__c, QuotetoInvoice__c, CreatedDate
                         from Quotes 
                         order by CreatedDate DESC)
                  from Opportunity
                  where Id IN :oppsId]) 
{

 if (opp.Invoices__r.AF_LastSyncTime__c==null) return 'blah blah blah'; 

 }

No error with query, but I get invalid foreign key relationship in the if statement....why is that?

Thanks.
Best Answer chosen by Sammy7
Prateek Singh SengarPrateek Singh Sengar
Hi Sammy,
From your query it seems that invoice is child of opportunity to access invoice records your syntax would be
for (Opportunity opp : [select Id, (select id, AF_LastSyncTime__c from Invoices__r),
                       (select Id, Invoice_No__c, QuotetoInvoice__c, CreatedDate
                         from Quotes 
                         order by CreatedDate DESC)
                  from Opportunity
                  where Id IN :oppsId]) 
{

for(Invoices__c inv: opp.Invoices__r)
{
 if (inv.AF_LastSyncTime__c==null) return 'blah blah blah'; 
}
}
Hope this helps.

Please note that i haven't verified the code for syntax errors.

 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Sammy,
From your query it seems that invoice is child of opportunity to access invoice records your syntax would be
for (Opportunity opp : [select Id, (select id, AF_LastSyncTime__c from Invoices__r),
                       (select Id, Invoice_No__c, QuotetoInvoice__c, CreatedDate
                         from Quotes 
                         order by CreatedDate DESC)
                  from Opportunity
                  where Id IN :oppsId]) 
{

for(Invoices__c inv: opp.Invoices__r)
{
 if (inv.AF_LastSyncTime__c==null) return 'blah blah blah'; 
}
}
Hope this helps.

Please note that i haven't verified the code for syntax errors.

 
This was selected as the best answer
Sammy7Sammy7
Great. Its because of the list...