APEX类列出要在VisualForce页面中使用的多个查询

时间:2017-12-19 20:20:28

标签: salesforce apex soql

* SF开发新手* 我正朝着这个目标努力;在商机上创建自定义按钮。按下该按钮时,VisualForce页面将显示与该机会(ID)有关系的2个自定义对象的记录。两个自定义对象都通过自定义查找字段与“商机”相关联。

作为一名新秀,我不确定我是否会在正确的位置开始。这就是我试图想到的。以下代码适用于第1项和第1项。下面#2。

  1. 创建查询单独数据的类方法。每种方法都是一个单独的查询/列表
  2. 包含编码以提取当前商机ID的变量
  3. 创建显示查询数据的VisualForce页面
  4. 创建触发APEX代码的按钮
  5. public with sharing class TestDisplayQueryList{ 
    
    public Opportunity currRec {get; set;}     
    public static List<Opportunity> oppRecords {get; set;} 
    public static List<Billing__c> billRecords {get; set;} 
    public static List<Service__c> servRecords {get; set;}   
    
    public TestDisplayQueryList(){ 
    currRec = [SELECT ID FROM Opportunity WHERE :ApexPages.currentPage().getParameters().get('id')];   
    oppRecords = [SELECT Name, StageName ID FROM Opportunity WHERE ID= :currRec];
    billRecords = [SELECT Name, Invoice ID FROM Billing WHERE Opportunity_Name_c= :currRec];  
    servRecords = [SELECT Name, Department ID FROM Service WHERE Opportunity_Name_c= :currRec];     
    }
    }
    

2 个答案:

答案 0 :(得分:0)

3。)有很多方法可以做到这一点,最简单的只是一个页面块表 https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_iteration_components.htm

将您的值作为您的控制器列表,每个列表都需要一个单独的页面块表。

4)。 创建自定义按钮或链接,您可以使用salesforce自定义按钮执行此操作,只需将其设为网址并将其传递给网址末尾的?id。这将自动导致代码运行,因为代码可以在页面初始化时运行。您将此按钮放在商机页面上。

如果你想在弹出窗口中显示信息(对于系统看起来更好看),你也可以做一个自定义按钮,只需做一个javascript按钮。有大量的在线教程,可以在javascript中为弹出窗口制作按钮。 javascript可以扩展到控制器并传递机会ID。

如果你遇到困难,请告诉我,祝你好运!

答案 1 :(得分:0)

说实话,你可以比你想象的更容易。

VisualForce拥有大量标准组件,可用于此类任务。

下面是一个示例VisualForce页面:

<apex:page standardController="Opportunity" extensions="OpportunityExt">

    <!-- Either include all related lists by setting the relatedList attribute to true -->
    <apex:detail subject="{!opportunity.Id}" relatedList="false"/>

    <!-- Or by Including the related lists seperately -->
    <apex:relatedList list="OpportunityLineItems" />
    <apex:relatedList list="OpportunityCompetitors" />

    <!-- To use a button to display a panel, create the panel to reRender after our button action is complete -->
    <apex:outputPanel id="thePanel">

        <!-- Then create the buttons -->
        <apex:commandButton action="{!showThePanel}" value="Show Panel" reRender="thePanel" rendered="{!NOT(showPanel)}"/>
        <apex:commandButton action="{!hideThePanel}" value="Hide Panel" reRender="thePanel" rendered="{!showPanel}"/>

        <!-- And inside that panel create a block that will only render when the flag is set -->
        <!-- Note the use of outputPanel and outputText differently -->
        <!-- OutputText leaves no container behind, outputPanel does so we need it for the "identifying panel" -->
        <apex:outputText rendered="{!showPanel}">

            <!-- You can then include the related lists you wanted -->
            <apex:relatedList list="OpenActivities" />

        </apex:outputText>

    </apex:ouputPanel>

</apex:page>

这将是该页面的扩展名:

public with sharing class OpportunityExt {

    private Opportunity theOpportunity;
    public Boolean showPanel {get; private set;}

    public OpportunityExt(ApexPages.StandardController controller) {

        // Get the Opportunity from the standard controller
        theOpportunity = (Opportunity) controller.getRecord();

        // By default don't display the panel - or do, it's your task
        showPanel = false;

    }

    public void showThePanel() {

        showPanel = true;

    }

    public void hideThePanel() {

        showPanel = false;

    }

}

以下是文档链接供您参考:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_detail.htm https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_relatedList.htm https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_commandButton.htm https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputPanel.htm https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputText.htm

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm