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
Pooja NekkalapudiPooja Nekkalapudi 

How to create a Save & new button in vf page

I already have VF page which has the Save and cancel button , I want to create a new button " Save & New ".. i do not know how to make it return to the page to create a new record 

public PageReference doFullSave(){
 // this is my Save method
}
public PageReference doFullSave1(){ // this is my save and new method which has to return to the save mode again 
   doFullSave();
  PageReference retPage = new PageReference('/'+ ApexPages.currentPage().getParameters().get('retURL'));
  PageReference returnPage = new PageReference(ApexPages.currentPage().getParameters().get('retURL'));
   return returnPage;
    }

VF page :
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!doFullSave}"/>
<apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/>
<apex:commandButton value="Save and New" action="{!doFullSave1}"/>
</apex:pageBlockButtons>

This is the url on the new record page 
sandbox--c.cs4.visual.force.com/apex/CallLog?CF00N6000000268Gm=test12345&CF00N6000000268Gm_lkid=a0wP00000010bHcIAI&retURL=%2Fapex%2FSL_MassEdit_CallLog%3FpId%3Da0wP00000010bHcIAI%26justView%3Dtrue%26page%3D0%26cbf%3Dnull%26cbaf%3Dnull%26qb%3DAll%26qs%3DAll%26qct%3DAll%26qtt%3D%26sortE%3DName%26sortD%3DASC%26cb_letterFilter%3DAll&scontrolCaching=1&sfdc.override=1
Best Answer chosen by Pooja Nekkalapudi
Rohit K SethiRohit K Sethi
Hi Pooja,

User below code for save and New button :
public PageReference doFullSave1(){
   doFullSave();
   String str = ApexPages.currentPage().getUrl().subStringAfter('/').substringBefore('?');
   return new PageReference('/apex/' + str).setRedirect(true);
}         
Note : This code first save your current record and after that redirect it to the same page after saving changes.


If this post solves your problem kindly mark it as solution.
Thanks.

All Answers

Rohit K SethiRohit K Sethi
Hi Pooja,

User below code for save and New button :
public PageReference doFullSave1(){
   doFullSave();
   String str = ApexPages.currentPage().getUrl().subStringAfter('/').substringBefore('?');
   return new PageReference('/apex/' + str).setRedirect(true);
}         
Note : This code first save your current record and after that redirect it to the same page after saving changes.


If this post solves your problem kindly mark it as solution.
Thanks.
This was selected as the best answer
Pooja NekkalapudiPooja Nekkalapudi
This worked perfect but after couple of save and new clicks , if i click cancel , my cancel button is not working it says URL No Longer Exists
Rohit K SethiRohit K Sethi
Hi Pooja ,

On click on save & new we are doing nothing just reload the current page after saving current record. 
I thing your cancel button calculating some url hacking and the expectin paramter from url and can not find expected URL that is why this is through error.

If still you have the same problem plz send the cancel button code so that I can resolve this issue.

Thanks.
Pooja NekkalapudiPooja Nekkalapudi
This is my cancel code :

public PageReference doCancel(){
        PageReference ref;
        retURL = ApexPages.currentPage().getParameters().get('retURL');
        if (retURL!=null && retURL.length()>0) 
        { 
            retURL = EncodingUtil.urlDecode(retURL, 'UTF-8');
            if(retURL.length()>4 && retURL.substring(0,4)=='http') ref = new PageReference(retURL);
            else
            {
                retURL = retURL.substring(1); 
                ref = new PageReference('/' + retURL);
            }
        } else {
            if (call.Id!=null) ref = new PageReference('/' + call.Id);
            else ref = new PageReference('/' + project.Id);
        }

        ref.setRedirect(true);
        return ref;
    }