I am looking at showing a subquery SOQL query in an Visualforce page. This is my SOQL Expression.
public ApexPages.StandardSetController setCon {
get {
if(setCon == null) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT Contact.Id, Opportunity.Id, Contact.FirstName, Contact.LastName, Contact.Phone,Contact.Account.Name, Contact.Email,Contact.Last_Contacted__c,Contact.Membership_Type__c,Opportunity.Call_Disposition__c, Opportunity.Sales_Stage__c,Opportunity.Name, Opportunity.CloseDate, Opportunity.StageName, Opportunity.CreatedDate FROM OpportunityContactRole where Opportunity.OwnerId=:Userinfo.getUserId()]));
}
return setCon;
}
set;
}
It got a message 'OpportunityContactRole is not supported in StandardSetController'. So I tried to get Opportunity and Contact info from Account...
So my SOQL query changed to:
SELECT Name, (SELECT Name, Phone, Email, Last_Contacted__c, Contact.Membership_Type__c FROM Account.Contacts) , (SELECT Call_Disposition__c, StageName, CreatedDate, CloseDate FROM Account.Opportunities) FROM Account where Id=:UserInfo.getUserId()])
but now in my Visualforce page I am not able to access the subquery fields:
<apex:page controller="SalesRepPageControllerV3" tabstyle="contact" sidebar="false" showChat="true" >
<apex:form id="theForm">
<apex:sectionHeader title="Sales Rep Page for {!$User.FirstName}"/>
<apex:pageBlock id="innerblock" mode="edit">
<apex:pageMessages />
<apex:pageBlock id="innerblock">
<apex:pageBlockSection id="pagesection">
<apex:pageBlockTable value="{!ContactOpportunity}" var="co" id="pageblocktable">
<apex:column headerValue="Created Date" value="{!co.Opportunity.CreatedDate}"/>
<apex:column headerValue="First Name" value="{!co.Contact.FirstName}"/>
<apex:column headerValue="First Name" value="{!co.Contact.LastName}"/>
<apex:column headerValue="Phone" value="{!co.Contact.Phone}"/>
<apex:column headerValue="Account Name" value="{!co.Contact.Account.Name}"/>
<apex:column headerValue="Email" value="{!co.Contact.Email}"/>
<apex:column headerValue="Last Contacted" value="{!co.Contact.Last_Contacted__c}">
</apex:column>
<apex:column headerValue="Membership Type" value="{!co.Contact.Membership_Type__c}"/>
<apex:column headerValue="Call Disposition">
<apex:inputField value="{!co.Opportunity.Call_Disposition__c}"/>
</apex:column>
<apex:column headerValue="Sales Stage">
<apex:inputField value="{!co.Opportunity.Sales_Stage__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" reRender="pageblocktable"/>
<apex:commandButton value="Cancel" action="{!cancel}" reRender="pageblocktable"/>
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:panelGrid columns="2">
<apex:commandLink action="{!previous}">Previous</apex:commandlink>
<apex:commandLink action="{!next}">Next</apex:commandlink>
</apex:panelGrid>
</apex:form>
</apex:page>
It does not understand the co.Opportunity and co.Contact as it is not a Account. And if I change it to co.Opportunities or co.Contacts it is an Arraylist which can only be accessed by repeat.
The problem with apex:repeat is it does not have column header values which I need to sort by. Please if someone can help please let me know how.
Heavily edited...
I don't think you can use a standard set controller for this. Using a custom controller I was able to get a consolidated list of opportunity contacts for the current user:
Page:
<apex:page controller="TestQueryController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!opportunities}" var="co">
<apex:column value="{!co.Opportunity.CreatedDate}"/>
<apex:column value="{!co.Contact.FirstName}"/>
<apex:column value="{!co.Contact.LastName}"/>
<apex:column headerValue="Stage">
<apex:inputField value="{!co.Opportunity.StageName}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</Apex:form>
</apex:page>
Controller:
public with sharing class TestQueryController {
List<OpportunityContactRole> myOCR;
public List<OpportunityContactRole> getOpportunities() {
if (myOCR == null) {
myOCR = [SELECT Contact.Id, Opportunity.Id, Contact.FirstName, Contact.LastName, Contact.Phone,
Contact.Account.Name, Contact.Email, Opportunity.Name,
Opportunity.CloseDate, Opportunity.StageName, Opportunity.CreatedDate
FROM OpportunityContactRole where Opportunity.OwnerId=:Userinfo.getUserId()];
}
return myOCR;
}
}
You will need to substitute in your custom fields. If you need sorting, this blog post gives one approach, though you can probably avoid repeat trips to the database using a wrapper class and the Apex Comparable interface. Finally, you'll need to implement your own save logic. Good luck!