4

I am trying to get Opportunities from Contacts but hitting the below error:

Save error: The configuration of your org has changed, please reload the page. Missing dependent object: Field: Opportunity.ContactId

This is the code:

    Contact contactObj = [SELECT Id,
                                 (SELECT Id,Status,CaseNumber,LastModifiedDate FROM Cases),
                                 (SELECT Id,Name,StageName,LastModifiedDate FROM Opportunities) 
                                 FROM Contact Where Id=:sobj.Id];


    List<SObject> opportunitySObjList = contactObj.Opportunities; //It shows error on this line.

I googled on this and looks like it is a salesforce bug but would like to know if anyone had any suggestion/help on this?

4
  • Try this solution success.salesforce.com/issues_view?id=a1p30000000T2hjAAC
    – Rao
    Mar 12, 2015 at 18:33
  • It says increasing the API version should work..but I am already on the version 31.0..
    – nick
    Mar 12, 2015 at 18:54
  • is you class on 31 or your page at 31? can you post your class are there inner classes/ wrappers in your code?
    – Rao
    Mar 12, 2015 at 19:00
  • @rao Both my apex classes and VisualForce at 31.0.
    – nick
    Mar 12, 2015 at 20:14

2 Answers 2

5

This field is a bug and should not be used. It is technical debt that SFDC has not cleaned up yet. Mary Scotton at SFDC commented on this here: http://gokubi.com/archives/mystery-contact-id-field-on-opportunity

You should query against Opportuinty, Contact, and OpportunityContactRole to get the related SObjects.

This SOQL will work and get you what you want:

List<Contact> contactObj = [SELECT Id,
                      (SELECT Id,Status,CaseNumber,LastModifiedDate FROM Cases),
                      (SELECT Id,OpportunityId,Opportunity.StageName, Opportunity.Amount, ContactId FROM OpportunityContactRoles) 
                     FROM Contact ];

System.debug(JSON.serialize(contactObj));
1
  • Glad to help. :) Please mark it as solved if it works so others see its solved. :) Mar 12, 2015 at 23:16
-1

This can be resolved by using Opportunities__r.

SELECT Id, (SELECT Id,Status,CaseNumber,LastModifiedDate FROM Cases),
(SELECT Id,Name,StageName,LastModifiedDate FROM Opportunities__r) 
FROM Contact Where Id=:sobj.Id

Much like how you would use __c to indicate a custom field, __r indicates a custom relationship.

1
  • 1
    The child relationship field on Contact for Opportunity is Opportunities only not Opportunity__r. Sep 3, 2015 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .