如何实现“Save &"新"VisualForce 页面中的功能 [英] How to implement "Save & New" functionality in a VisualForce Page

查看:21
本文介绍了如何实现“Save &"新"VisualForce 页面中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是如何保存记录

I know that this is how to save a record

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

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

Now I want a button to save the current record and reset the form to input another record.

这样的东西...

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

推荐答案

新记录页面的 URL 是 {org URL}/{3 letter object prefix}/e?".

The URL for the new record page is the {org URL}/{3 letter object prefix}/e?".

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

You could define your save method as follows, where m_sc is a reference to the standardController passed to your extension in it's constructor:

  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 引用作为参数:

To use your controller as an extension, modify it's constructor to take a StandardController reference as an argument:

public class TimeSheetExtension
{
  ApexPages.standardController m_sc = null;

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

  //etc.

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

Then just modify your <apex:page> tag in your page to reference it as an extension:

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

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

Note that you don't need Extension in the class name, I just did that to be sensible. You shouldn't need to modify anything else on your page to utilise this approach.

这篇关于如何实现“Save &amp;"新"VisualForce 页面中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆