+ Start a Discussion
sales4cesales4ce 

Display All Contacts associated with an opportunity--Help!

Hi,

 

I have an issue. I want to display in a visualforce page all contacts associated with an opportunity.Some how i am unable to get the data

 

Here is my VF Code :

 

<apex:page StandardController="Opportunity" extensions="OpptyReport" action="(!oppcontacts}">
   
    <apex:pageBlock Title="Sample Report" >
        <apex:pageblockSection Columns="2" >
            <apex:pageBlockTable value="{!AssociatedContacts}" var="cts">           
                <apex:column headerValue="Contact Name">
                    <apex:outputText value="{!cts.Contact.Name}">
                    </apex:outputText>
                 </apex:column>
                 <apex:column headerValue="Contact Role">
                    <apex:outputText value="{!cts.Role}">
                    </apex:outputText>
                 </apex:column>
            </apex:pageBlockTable>                                           
        </apex:pageblockSection>           
    </apex:pageBlock>
</apex:page>

 

Here is My Apex Class:

 

public class OpptyReport {

    public OpptyReport(ApexPages.StandardController controller) {

    }
    
   public List<OpportunityContactRole> cts=new List<OpportunityContactRole>();
   
   public void oppcontacts(){
       cts=[Select contactId, Contact.Name, Role from OpportunityContactRole where  OpportunityId=:Apexpages.Currentpage().getparameters().get('Id')];
       
       //return null;
       }
    public List<OpportunityContactRole> getAssociatedContacts(){
        //System.debug('Contact Role=' + cts.Contact.name);
        return cts;
    
    }

Any help/idea is highly appreciated.

 

Thanks in advance

 

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hello Sales4ce;

You need not to have a controller to get data from OpportunityContactRoles.

You can do it as below.

<apex:page standardController="Opportunity">

<apex:pageBlock Title="Sample Report" >

<apex:pageblockSection Columns="2" >

<apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" var="cts">

<apex:column headerValue="Contact Name">

<apex:outputText value="{!cts.Contact.Name}"/>

</apex:column>

<apex:column headerValue="Contact Role">

<apex:outputText value="{!cts.Role}"/>

</apex:column>

</apex:pageBlockTable>

</apex:pageblockSection>

</apex:pageBlock>

</apex:page> 

 

 

 

Message Edited by prageeth on 03-15-2010 08:45 PM

All Answers

prageethprageeth

Hello Sales4ce;

You need not to have a controller to get data from OpportunityContactRoles.

You can do it as below.

<apex:page standardController="Opportunity">

<apex:pageBlock Title="Sample Report" >

<apex:pageblockSection Columns="2" >

<apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" var="cts">

<apex:column headerValue="Contact Name">

<apex:outputText value="{!cts.Contact.Name}"/>

</apex:column>

<apex:column headerValue="Contact Role">

<apex:outputText value="{!cts.Role}"/>

</apex:column>

</apex:pageBlockTable>

</apex:pageblockSection>

</apex:pageBlock>

</apex:page> 

 

 

 

Message Edited by prageeth on 03-15-2010 08:45 PM
This was selected as the best answer
sales4cesales4ce

Hello Prageeth,

 

Thanks for your quick reply!

It was really Helpful.

 

Can i know, why we do not need any controller on opportunitycontactrole?

 

Also, if for instance assume that i want to query and get only contacts based on their role. then how do i do this?

 

ex: I want to get all contacts asociated with an opportunity whose role is 'Decision Maker'.

how can i achieve this without a controller?

 

Thanks,

Sales4ce

prageethprageeth

Hello Sales4ce;

OpportunityContactRole has a child relationship with Opportunity. So you can get the complete list of "OpportunityContactRoles" by using the method "{!Opportunity.OpportunityContactRoles}".

For an exmple you can get the full list of "OpportunityLineItems" by using the method "{!Opportunity.OpportunityLineItems}". 

 

As you have asked in your second question, I don't think that you could filter the result by the "role".

If you could ask it in the forum as a new post, sometimes somebody will show you an alternative way. However in your case, since you are using a "pageBlockTable" you can avoid rendering the "column" by using the "rendered" attribute as below. (But I think there could be a better alternative)

 

<apex:page standardController="Opportunity">

<apex:pageBlock Title="Sample Report" >

<apex:pageblockSection Columns="2" >

<apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" var="cts">

<apex:column rendered="{!cts.Role=='Decision Maker'}" headerValue="Contact Name">

<apex:outputText value="{!cts.Contact.Name}"/>

</apex:column>

<apex:column rendered="{!cts.Role=='Decision Maker'}" headerValue="Contact Role">

<apex:outputText value="{!cts.Role}"/>

</apex:column>

</apex:pageBlockTable>

</apex:pageblockSection>

</apex:pageBlock>

</apex:page>