我正在寻找在Visualforce页面中显示子查询SOQL查询。这是我的SOQL表达式。
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;
}
它收到一条消息'StandardSetController中不支持OpportunityContactRole'。所以我试图从帐户中获取机会和联系信息......
所以我的SOQL查询改为:
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()])
但现在在我的Visualforce页面中,我无法访问子查询字段:
<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>
它不理解co.Opportunity和co.Contact,因为它不是帐户。如果我将其更改为co.Opportunities或co.Contacts,则它是一个只能通过重复访问的Arraylist。
apex:repeat的问题是它没有我需要排序的列标题值。如果有人可以帮忙,请告诉我如何。
答案 0 :(得分:3)
重编辑......
我认为你不能使用标准的控制器。使用自定义控制器,我能够获得当前用户的综合机会联系人列表:
页:
<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>
控制器:
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;
}
}
您需要在自定义字段中替换。如果您需要排序,this blog post提供了一种方法,尽管您可以避免使用包装类和Apex Comparable接口重复访问数据库。最后,您需要实现自己的保存逻辑。祝你好运!
答案 1 :(得分:0)
您拥有的是每个嵌套查询的内部表。 pageblocktable只能迭代单个表。
请按照下面的代码示例进行操作。在帐户列表中选择您的帐户,然后使用一个pageblocktable和两个嵌套的pageblocktables迭代列表。
控制器
public List<Account> _accounts;
public list<Account> Accounts {
get {
if (_accounts == null) {
_accounts = [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()];
}
return _accounts;
}
}
Visualforce
<apex:pageBlockTable value="{!Accounts}" var="acc" id="pageblocktable">
<apex:column headerValue="Account Name" value="{!acc.Name}"/>
<apex:pageBlockTable value="{!acc.Opportunities}" var="ops" id="opportunities">
<apex:column headerValue="Created Date" value="{!ops.CreatedDate}"/>
<apex:column headerValue="Call Disposition">
<apex:inputField value="{!ops.Call_Disposition__c}"/>
</apex:column>
<apex:column headerValue="Sales Stage">
<apex:inputField value="{!ops.Sales_Stage__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!acc.Contacts}" var="con" id="contacts">
<apex:column headerValue="First Name" value="{!con.FirstName}"/>
<apex:column headerValue="First Name" value="{!con.LastName}"/>
<apex:column headerValue="Phone" value="{!con.Phone}"/>
<apex:column headerValue="Account Name" value="{!con.Account.Name}"/>
<apex:column headerValue="Email" value="{!con.Email}"/>
<apex:column headerValue="Last Contacted" value="{!con.Last_Contacted__c}" />
<apex:column headerValue="Membership Type" value="{!con.Membership_Type__c}"/>
</apex:pageBlockTable>
</apex:pageBlockTable>