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
Thomas Reinman 18Thomas Reinman 18 

'Save and New' commandButton for a Single Record

I have a requirement where I need to navigate to a new Visualforce page after saving the new record. Hence, I added a simple extension to try to resolve these two actions. 

User-added image

The page is redirecting to the other Visualforce page, but the record is not being saved. 
I've noticed that the .save() method doesn't really factor in Validation rules the way the standard {!save} action DOES. 

For this requirement, I've also tried using an INSERT DML statement instead of the .save() method, and I've even tried to use the standard {!save} method in conjunction with some Javascript redirection. Nothing seems to save the record. 

Another important detail -- the data I'm trying to save as a new record is coming from three separate Visualforce pages. I know the data is stored in the viewstate because the standard 'Save' button creates a new record. However when attempting to use a custom 'save' via DML, an error pops up listing the Required fields that are missing (which were previously filled out on the 1st page).

Any suggestions....?
Thomas Reinman 18Thomas Reinman 18
Note -- forgot to mention, this is a requirement for NEW records, so I can't do a SOQL query on the ID. I'd also like to try to avoid using SOQL and Upsert anyways. Thanks
Raj VakatiRaj Vakati
Your code is correct ..  Try this one and see is there any expcetion your are getting in error logs 

 
public class LeadSaveExtension3 {
    ApexPages.StandardController sController;
    public LeadSaveExtension3(ApexPages.StandardController controller) {
        sController = controller;
    }

    public PageReference saveAndNew() {
        sController.save(); 
        PageReference pg = new PageReference('/apex/Sample');
        pg.setRedirect(true);
        return pg;
    }
}


<apex:page standardController="Lead" extensions="LeadSaveExtension3">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            
            <apex:commandButton value="Save & New" action="{!saveAndNew}"/>
         </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputField value="{!Lead.FirstName}"/>
                <apex:inputField value="{!Lead.LastName}"/>
                <apex:inputField value="{!Lead.Email}"/>
            </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

 
Thomas Reinman 18Thomas Reinman 18
Thanks Raj, I’m going to try this when I get home and I’ll let you know the results. 
Raj VakatiRaj Vakati
Worked ? 
Thomas Reinman 18Thomas Reinman 18
HI Raj,
Thanks for the follow up, unfortunately I got the same results. The page does redirect but no record is saved. Do you have any other suggestions?
Raj VakatiRaj Vakati
can you check debug logs ..