+ Start a Discussion
dave6dave6 

How to query Contact's Opportunities?

I'm using the enterprise WSDL in Java and I need to query all the Opportunities a Conatct has and set the Account ID on each Opportunity found.  I see the Contact object has serveral Opportunity related methods, getOpportunities(), getOpportunity__c(), getOpportunities1__r()...getOpportunities5__r()etc.  What should I use to do this?  getOpportunities() returns a QueryResult but it seems to be always null.  How can I get this data?

 

If I could have gotten this linked/joined data when I queried for the Contact that might be okay too but I'm not sure how to get/query for joined data in SF.

 

This is probably a basic question but could really use some help...quite new to SF.

Santhosh KumarSanthosh Kumar

All those getOpportunites* methods are custom relationships your team has created as Opportunity doesn't have a contact relationship to Opportunity by default. So check with the team, which is the correct relationship for you to query the data.

Daniel HaselhanDaniel Haselhan

Since your team has created custom relationships to each of Opportunites, you will need to not only specify the data you want from the Contact in your query but also the data you need from each Opportunity on the Contact.

 

E.g.


Contact newContact = Connection.query("Select Name, Id, Opportunities1.Name, Opportunities1.Amount, Opportunities2.Name, Opportunities3.Name, ....... FROM Contact WHERE Id = 'XDASDASDA')";


Then as an example, you would use:
String opportunity1Name = newContact.getOpportunities1().getName();

to get the name of Opportunities1

 

Hope this helps!