如何实现“保存”新” VisualForce 页面中的功能

发布于 2024-12-27 21:42:17 字数 261 浏览 2 评论 0原文

我知道这就是保存记录的方法

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

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

像这样的东西...

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

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.

Something like this...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2025-01-03 21:42:17

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

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

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

请注意你不需要扩展在类名称中,我这样做只是为了明智起见,您不需要修改页面上的任何其他内容即可使用这种方法。

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

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

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.

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.

静若繁花 2025-01-03 21:42:17

理想情况下,您可以使用 ApexPages.Action 类来实现此目的。但当我尝试使用它时,却发现它的问题太多了。已经有一段时间了,因此您可能想使用 {!URLFOR($Action.Account.New)} 操作来使用它。

的工作原理只是使用PageReference将用户重定向到“新”URL。

例如,如果这是针对帐户的,

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

    PageReference pageRef = new PageReference('/001/e');
    return pageRef;
}

Ideally, you could use the ApexPages.Action class for this. But when I've tried to use it, it's been too buggy. It's been a while, so you might want to play with it using the {!URLFOR($Action.Account.New)} action.

What will work is simply using a PageReference to redirect the user to the "new" URL.

For example, if this were for Accounts,

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

    PageReference pageRef = new PageReference('/001/e');
    return pageRef;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文