自定义按钮或使用标准控制器链接到Visualforce页面

时间:2016-04-05 08:51:03

标签: salesforce visualforce

我使用visual force创建页面。如何为标准页面创建自定义按钮/链接。单击自定义按钮/链接后,我需要打开页面。这该怎么做。我的代码在

之下
<apex:page standardcontroller="Account" tabstyle="Account" extensions="MyExtension" >
 <apex:form id="form1">
  <apex:commandlink action="{!showForm2}" value="Show the Form" rendered="{!showForm}" reRender="form2,op1"/>
</apex:form>
<apex:outputpanel id="op1">
 <apex:form id="form2">
 <apex:sectionheader title="Account Details" subtitle="{!if(Account.Id==null,'New Account',Account.Name)}"></apex:sectionheader>
<apex:pageblock mode="edit" id="leadPB" title="Account Edit">

 <apex:pageblockbuttons >
<apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
 <!-- If you wish to implement Save & New functionality you will have to write an Apex Extension with your own Save & New Method -->
 <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
 </apex:pageblockbuttons>

        <apex:pageBlockSection >
            <apex:inputtext value="{!Account.LastName}" label="Customer Name"/>
            <apex:inputtext value="{!Account.PersonMobilePhone}"/>
            <apex:inputtext value="{!Account.CustomLandLine__c}"/>
            <apex:inputField value="{!Account.City__c}"/>
            <apex:inputField value="{!Account.PersonEmail}"/>
             <apex:inputField value="{!Account.Source__c}"/>

            <!-- <apex:commandButton action="{!save}" value="Save!"/>-->
        </apex:pageBlockSection>
    </apex:pageBlock>
<apex:pageMessages />
</apex:form>
</apex:outputpanel>
</apex:page>`

我的班级代码

public with sharing class MyExtension {
    private ApexPages.StandardController sc;
    public MyExtension(ApexPages.StandardController sc) {
        this.sc = sc;
    }
    public PageReference save() {
        Account a = (Account) sc.getRecord();
        a.OwnerId = [select Id from User where LastName = 'Kapoor'].Id;
        a.OwnerId = [select Id from User where FirstName = 'Raoul'].Id;
        return sc.save();
    }
public boolean showForm{get;set;}

// default the var to false;
showForm = false;

public void showForm2(){
 showForm = true;
}
}

但它显示以下错误

控制器

  

错误:编译错误:意外标记:第15行第9行的'='

在我的页面中,我收到了此错误

  

未知方法'AccountStandardController.showForm2()'

如何解决这个问题

1 个答案:

答案 0 :(得分:2)

请将showForm = false;放在this.sc = sc;下方。构造函数内的所有内容都用于初始化/默认值。
您遇到第二个错误,因为控制器扩展未成功保存。修复第一个错误后,两个错误都会消失。