Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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

1 Answer

0 votes
by (32.1k points)
edited by

The save method can be defined such that where m_sc is a reference to the standardController which is 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);

  }

Now since you want to your controller as an extension, you can 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;

  }

Now, you need to modify your <apex:page> tag in your page in order 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>

Check out this Salesforce course to enhance your career!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...