Hello and thanks for taking the time to look into my question.

I had an earlier question and Jesse Altman was kind enough to give me a great response.

I am using Force IDE and the salesfore.schema to find the appropriate variables, but I am getting an 'Invalid foreign key relationship Opportunity.service_contracts__r.

This is my code:

for(Opportunity opp:Trigger.new){
        if(opp.StageName == 'PO In (100%)' && opp.Service_Contracts__r.size == 0){


This is my schema:

The opportunity Child Relationship schema

I must be missing something, and I do not know how I can test this.

Thanks again for any pointers.


Oops. It should be opp.Service_Contracts__r.size()

I missed the parentheses in my other post. Apparently Salesforce doesn't give you a very good error message for this situation.

Also, just as quick warning, you may need to load the list from the database (if you notice it always coming back as null). I can't remember now if the list is populated in a trigger context or not.

| improve this answer | |
  • Thanks Jesse! I will try it out tomorrow morning, as I have just gotten home and cannot change the network security. What do you mean by load the list? – jnoel10 Feb 12 '13 at 19:21
  • 4
    Related objects (either parent or child) are not populated in Trigger.new/old. Best practice would be to do a "collect" step with the initial for loop to get only the records you want to work with, make the DB trip with SOQL, then go back for the child records with a second loop. – pchittum Feb 12 '13 at 21:44
  • Thanks Peter, I will do just that. With this loop though I wouldnt then be calling opp.Service_Contracts__r.size() correct? Just the list created by the for loop? – jnoel10 Feb 13 '13 at 13:17
  • @Peter I couldn't remember if they were populated or not. Good to know! – Jesse Altman Feb 13 '13 at 13:23
  • @jnoel10 - I updated my original answer on your original question to show the more correct code - salesforce.stackexchange.com/questions/8341/… – Jesse Altman Feb 13 '13 at 13:23

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.