in Controller i get the ContactRoles from opportunity through a listview but how to display ContactRoles in vf page

 public AccountListViewExtension(ApexPages.StandardSetController controller) {
         filters = controller.getListViewOptions();
         selectedFilterId = controller.getFilterId();
         FilterId2=[SELECT Name,id FROM ListView WHERE id = :selectedFilterId ];

         List<SObject> listViews = controller.getRecords();
         for(SObject listView : listViews) {
        String a = listView.id;

        List<OpportunityContactRole> a1 = [Select id,Role From OpportunityContactRole Where OpportunityId =:a];  

<apex:inputfield value={!a1}>
  • where exactly are you stuck, there is plenty of documentation and Trailhead tutorials on line for you to learn from – glls Mar 8 '18 at 22:38
  • Lookup in vf page to display contactroles i.e a1 in the above controller – khan Mar 8 '18 at 23:07
  • Please be nicer, @glls. Reread what you wrote- it's not a good way to attract new developers. – DavidSchach Mar 8 '18 at 23:33
  • 2
    But we do need them to post adequate questions with at least the information needed to help. Questions like how do I do a “class” of this with not demonstrated effort should be pointed to trailhead. We don’t need to rehash training here – Eric Mar 9 '18 at 8:27

There isn't really enough information in your question to propose a single solution, so here are some suggestions.

Right now, you're running a SOQL query in a loop, which will generate limits exceptions. You also do not seem to have a data structure available to store the OpportunityContactRoles and make them available to the Visualforce page.

A better approach is to collect the Opportunity Ids in a set and perform a single query. Please note that the code below is an example and will need to be adapted to your situation.

// As an instance variable...
List<Opportunity> opps { get; set; }

// In the constructor....
Set<Id> setOfIds = new Set<Id>();
// Iterate over the SetController and put all Opportunity Ids in the Set.
// Then query once:
opps = [SELECT Id, Name, (SELECT Id, Role FROM OpportunityContactRoles)
        FROM Opportunity
        WHERE Id IN :setOfIds];

You then have the data in a single list of Opportunities, each with a List<OpportunityContactRole> property called OpportunityContactRoles, available in your controller.

You can then use the standard Visualforce iteration components, like <apex:repeat> and <apex:pageBlockTable>, to iterate over and display this information. You will likely want a nested repeat, so that one iterates over the Opportunities and one over the OpportunityContactRoles:

<apex:repeat value="{! opps }" var="opp>
    <apex:repeat value="{! opp.OpportunityContactRoles }" var="ocr">
        <apex:outputField value="{! ocr.Id }" />
        <!-- Use more output components to show values from the Opportunity Contact Roles -->

You cannot supply a List<OpportunityContactRole> a1 to an input or output field component; you must use an iteration component with nested output components to show the specific properties you wish. You also must ensure that a getter is defined for the instance variable to make it available to the page.

For more information about how to show this information, see the following sections of the Visualforce Developer Guide:

Not the answer you're looking for? Browse other questions tagged or ask your own question.