4

I want to return all the opportunity contact roles for each opportunity where it's roles for an Opportunity, and I want to access a field in the contact role. my query looks ok, but I can't access the fields directly in Apex. Also, how do I access this in Visualforce?

Help?

        Opportunity opp = [SELECT id, Name, StageName,
    (select contact.name, contact.phone from OpportunityContactRoles where isPrimary = true  )  
    FROM Opportunity
    where id = '006e0000003V3NNAA0'];

    system.debug(opp.OpportunityContactRoles.contact.id);

Error:

Invalid foreign key relationship: Opportunity.OpportunityContactRoles

Visualforce:

<apex:outputText value="{!opps.???????

6

The problem is in how you're accessing the Opportunity Contact Roles. Your query is returning this data as a list; you need to select a specific Opportunity Contact Role.

Try this:

System.Debug(opp.OpportunityContactRoles[0].Contact.Name);

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.