3

我正在查看在 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;
    }

它收到一条消息“标准集控制器不支持 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 的问题是它没有我需要排序的列标题值。如果有人可以帮助,请告诉我如何。

4

2 回答 2

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;
    }
}

您将需要在您的自定义字段中进行替换。如果您需要排序,这篇博文提供了一种方法,尽管您可以使用包装类和 Apex Comparable接口避免重复访问数据库。最后,您需要实现自己的保存逻辑。祝你好运!

于 2012-08-17T21:54:39.760 回答
0

您拥有的是每个嵌套查询的内部表。pageblocktable 只能遍历单个表。

按照下面的代码示例。在 Account 列表中选择您的帐户,然后使用一个 pageblocktable 和两个嵌套的 pageblocktable 遍历列表。

控制器

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;
    }
}

视觉力量

<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>
于 2012-08-17T01:27:26.920 回答