Save and New feature using Apex in Salesforce

Save and New feature using Apex in Salesforce

Visualforce page:

<apex:page standardController=”Contact” extensions=”Sample”>
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value=”Save” action=”{!save}”/>
            <apex:commandButton value=”Save & New” action=”{!saveAndNew}”/>
            <apex:commandButton value=”Cancel” action=”{!cancel}”/>
        </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputField value=”{!Contact.FirstName}”/>
                <apex:inputField value=”{!Contact.LastName}”/>
                <apex:inputField value=”{!Contact.Email}”/>
            </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex Class:

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


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

Cheers!!!

Leave a Reply