I am getting an error saying Save error:Invalid foreign key relationship: Opportunity.quotes

List<Opportunity> oppsFromDb =  [SELECT
                                 id, stageName, name, accountid, owner__c, 
                                 end_user_contact__c, recordTypeId, ownerId,
                                 (SELECT id FROM Service_Contracts__r), 
                                 (SELECT id, Agreement_Term__c, Transcoding_Overage__c, Media_Storage_Overage__c,Data_Transfer_Overage__c,
                                  Equipment_Totals__c, Set_Up_Fee_Totals__c, Monthly_Recurring_Totals__c, Commencement_Date__c,
                                  Media_Object_Overage__c, Media_Request_Storage_Overage__c, Entitled_Media_Storage__c,
                                  Entitled_Data_Transfer__c, Entitled_Transcoding__c, Entitled_Media_Object__c, Entitled_Media_Reqest__c From quotes)
                                 id IN:oppIds

The call is to the quote is :

for(Opportunity goodOpps: oppsFromDb){      
  SContract.add(new ServiceContract(Quote__c = goodOpps.quotes.id, etc...));}

Do i need to create seperate lists for the quote and the Service Contract? Making it 3 seperate SOQL queries and then having to run separate queries to the lists when I need to get information specific to the parent opportunity?


  • 1
    Side note.. In java ending a line without a ';' usually ends up in error, in SQL though it is ok and allows for neatness. Does Apex allow lines that do not end with ';' ( I imagine so since i'm not getting a million errors but just wanted to confirm)
    – jnoel10
    Feb 21, 2013 at 16:37
  • Your first "line" (using that term loosely) in Apex would simply be that entire statement for your List. It will allow you to enter line breaks and continue your logic on the next line while considering it all a single statement. Feb 21, 2013 at 16:44

1 Answer 1


You aren't accessing the quotes object properly there. The quotes object is actually a List<Quote>. You would need to do something like goodOpps.quotes.get(0).id to access that properly (assuming you want the first ID)

  • ahh, would I assume i should be able to add an additional WHERE CLAUSE for the quotes so.. SELECT ... FROM QUOTES WHERE iSynced == TRUE]; that way I make sure i only have a .quote.get(0).id as the correct quote
    – jnoel10
    Feb 21, 2013 at 17:02
  • 1
    Yep, you can definitely do that if you know for sure that will only return the exact quote you want Feb 21, 2013 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.