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!!!

4 comments:

  1. I'm new in salesforce and apex, please share test class for above controleer

    ReplyDelete
    Replies
    1. We can't write test class for standardControllers.

      Delete
    2. Hi Krish We Can't Write Test Class For Standard Controller

      Delete