4

我知道这是如何保存记录

<apex:commandButton action="{!save}" value="Save"/>

现在我想要一个按钮来保存当前记录并重置表单以输入另一条记录。

像这样的东西...

<apex:commandButton action="{!SaveAndNew}" value="Save & New"/>
4

2 回答 2

3

新记录页面的 URL 是 {org URL}/{3 个字母对象前缀}/e?"。

您可以按如下方式定义您的保存方法,其中 m_sc 是对在其构造函数中传递给您的扩展的标准控制器的引用:

  public Pagereference doSaveAndNew()
  {
    SObject so = m_sc.getRecord();
    upsert so;

    string s = '/' + ('' + so.get('Id')).subString(0, 3) + '/e?';
    ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info, s));
    return new Pagereference(s);
  }

要将控制器用作扩展,请修改它的构造函数以将 StandardController 引用作为参数:

public class TimeSheetExtension
{
  ApexPages.standardController m_sc = null;

  public TimeSheetExtension(ApexPages.standardController sc)
  {
    m_sc = sc;
  }

  //etc.

然后只需修改<apex:page>页面中的标签以将其作为扩展名引用:

<apex:page standardController="Timesheet__c" extensions="TimeSheetExtension">
  <apex:form >
    <apex:pageMessages />
    {!Timesheet__c.Name}
    <apex:commandButton action="{!doCancel}" value="Cancel"/>
    <apex:commandButton action="{!doSaveAndNew}" value="Save & New"/>
  </apex:form>
</apex:page>

请注意,您不需要在类名中使用 Extension,我只是这样做是明智的。您无需修改​​页面上的任何其他内容即可使用此方法。

于 2012-01-19T06:00:50.523 回答
2

理想情况下,您可以为此使用 ApexPages.Action 类。但是当我尝试使用它时,它太麻烦了。已经有一段时间了,所以你可能想用这个{!URLFOR($Action.Account.New)}动作来玩它。

可行的方法是简单地使用 aPageReference将用户重定向到“新”URL 。

例如,如果这是针对 Accounts 的,

public PageReference SaveAndNew() {
    // code to do saving goes here

    PageReference pageRef = new PageReference('/001/e');
    return pageRef;
}
于 2012-01-19T05:49:04.937 回答