2012-08-16 48 views
2

我在看显示在Visualforce页面的子查询SOQL查询。这是我的SOQL表达式。顶点SOQL子查询中Visualforce

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。

先端的问题:重复是没有,我需要排序的列标头值。请如果有人能帮助,请让我知道如何。

回答

2

大量编辑...

我不认为你可以使用一组标准的控制器此。使用自定义控制器我能得到机会接触的合并列表为当前用户:

enter image description here

页:

<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接口重复访问数据库。最后,你需要实现你自己的保存逻辑。祝你好运!

+0

感谢metadaddy !!! :) – 2012-08-18 01:09:41

+0

不客气...如果它适合你,请注册并将其标记为答案。 – metadaddy 2012-08-18 01:31:19

+0

它的工作原理,但我希望将联系人和机会都放在一个pageBlockTable中,然后我可以对其进行排序。这有可能吗?还有其他的替代办法吗? – 2012-08-18 03:51:16

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> 
+0

万分感谢杰拉德!我会尝试和回复,如果我仍然有问题。 – 2012-08-17 03:25:39

+0

嗨杰拉德出于某种原因,它只是显示我的帐户名称字段和其他人。你也许知道为什么? – 2012-08-17 07:59:16