2

Need help retrieving all the partners from an opportunity given its id. This is what I thought it be :

List<Opportunity> temp = [Select (Select Role from OpportunityPartners)
From Opportunity where id='12345567'];

Not working though...and not sure what's wrong either

4
  • Are you getting any sort of error? Aug 23, 2016 at 1:34
  • Just saying the syntax is incorrect Aug 23, 2016 at 1:44
  • Hmm, try adding at least one field to the outer query - say Id - [Select Id, (Select Role from OpportunityPartners) From Opportunity where id='12345567'] Aug 23, 2016 at 1:45
  • @user2582622 If any of the below answers addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.
    – Adrian Larson
    Aug 24, 2016 at 21:44

3 Answers 3

5

Here is a script you may find useful to run through Execute Anonymous:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
{
    String name = relation.getRelationshipName();
    if (name != null && name.contains('Partner'))
        system.debug(name + ' (' + relation.getChildSObject() + ')');
}

This script yields three relationships:

AccountPartners (AccountPartner)
OpportunityPartnersFrom (OpportunityPartner)
Partners (Partner)

It seems to me you want door #2, which would make your query:

SELECT (SELECT Role FROM OpportunityPartnersFrom) FROM Opportunity
1
  • Although since you added in a nice script, +1 Aug 23, 2016 at 21:28
4

Change your query to:

List<Opportunity> temp = [Select (Select Role from OpportunityPartnersFrom)
From Opportunity where id='12345567'];

From the Opportunity context that's it's API Name

3

If you do not need any fields from the Opportunity then you can query the OpportunityPartners object directly:

List< OpportunityPartner > temp = [Select Role from OpportunityPartner
Where OpportunityID ='12345567'];

Otherwise you will need to use the correct API Name of the relationship

List<Opportunity> temp = [Select (Select Role from OpportunityPartnersFrom)
From Opportunity where id='12345567'];
6
  • Shouldn't the sObject in your first query be OpportunityPartner, not OpportunityPartners?
    – Adrian Larson
    Aug 23, 2016 at 2:02
  • 1
    @AdrianLarson - yup was still in edit of the answer then everyone showed up lol. Glad we have this problem......
    – Eric
    Aug 23, 2016 at 2:04
  • Fastest fingers in the west! Yay participation.
    – Adrian Larson
    Aug 23, 2016 at 2:04
  • @AdrianLarson (And Eric) Definitely a dead heat on this one! (I like the alternative solution too) Aug 23, 2016 at 21:26
  • How would I add an Opportunity Partner object to a specific opportunity? Assuming I have the id of the opportunity Aug 23, 2016 at 21:40

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.