function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AVINASH UPADHYAAVINASH UPADHYA 

Save And New

Hi Guys,

I am trying to implement a "Save & New" button. My approach for this is use standard controller for Save functionality, and i am writing a new controller extension for Save & New functionality.

My Save&New functionality is behaving little Weird.. For the 1st time it will save the User entered name and if I press save&new for the remaining time it will save Records ID in place of Name even if user entered the name.
Also for me it doesn’t look like preferred approach. Please comment if you find anything with the below codes.
 
 
<apex:page standardController="Family_Member_Details__c" sidebar="false" showHeader="false" extensions="FamilyDetailExtender">
<apex:form >
<apex:pageMessages />
<center><font size="5"><b><p>Family Information
</p> 
</b>
</font>
</center>

<apex:pageBlock >
<table style="width:100%">
   <tr><font size="3">
       <b> Family Member Name: </b><br/><apex:inputfield value="{!Family_Member_Details__c.Name}"/></font></tr>
    </table>
    </apex:pageBlock>
    <apex:commandButton action="{!Save}" value="Save"/>
    <apex:commandButton action="{!SaveANDNew}" value="Save and New"/>
    </apex:form>
</apex:page>

Controller extension:
public class FamilyDetailExtender {
//Variable to get the Id from the URL of which is passed from previous program.
Public final Id PageId;
public final Id ApplicationForm;

Public Family_Member_Details__c FamilyDetail1;

    
    Public FamilyDetailExtender(Apexpages.StandardController Fmly){
    FamilyDetail1 = (Family_Member_Details__c)Fmly.getRecord();
        PageId = System.currentPagereference().getParameters().get('PageId');

        FamilyDetail1.Application_Form__c  = [select Id,Name from College_Aid_Application_Form__c where Id = :PageId LIMIT 1].Id;

//Storing the Application ID so that this can be used when user press save and new.

        ApplicationForm = FamilyDetail1.Application_Form__c;
    }


// Write a methode for Save and New Custom button.

Public PageReference SaveANDNew(){

    system.debug('----------------------------------------->'+ApplicationForm);
    // This is the required field and since we masking this field in screen, in program we populating Application ID when user press Save and new.
 FamilyDetail1.Application_Form__c =  ApplicationForm;

//Insert is used to impliment the Save functionality.
Insert FamilyDetail1;

// After saving the record below code is expected to load the page with blank values so that user can enter the new value.
    FamilyDetail1 = new Family_Member_Details__c();
    PageReference FamilyPage1 = Page.FamilyDetail;
     
            // FamilyPage1.getParameters().put('Family_Member_Detail__r' , ClgAppForm.Name);
             FamilyPage1.getParameters().put('PageId' , PageId);
  
            
    return FamilyPage1;
    
   
}
}

Thanks guys for the awesome support :)
David ZhuDavid Zhu
You may use the following code snippet. setRedirect(true) is client side redict and it will will flush view state. 
Public PageReference SaveANDNew(){

    system.debug('----------------------------------------->'+ApplicationForm);
    // This is the required field and since we masking this field in screen, in program we populating Application ID when user press Save and new.
    FamilyDetail1.Application_Form__c =  ApplicationForm;

    //Insert is used to impliment the Save functionality.
    Insert FamilyDetail1;

    // After saving the record below code is expected to load the page with blank values so that user can enter the new value.
        PageReference newpage = new PageReference('/apex/FamilyDetail');  //if VF page name is not familydetail, replace it with your.
        newpage.setRedirect(true);
        return newpage 
   
}