本文介绍了如何实施“保存并保存"新" 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.

类似这样的事情...

Something like this...

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

推荐答案

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

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

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

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.

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

09-15 06:53