0

in the below controller class save button functionality working,but i need to write save&New (SV)button functionality..please suggest me the approach to complete this functionality.

public class EmployeeController {

   public Employee__c emp{
   get{
    if(emp==null)
    emp=new Employee__c();
    return emp;
    }
    set;
    }
    public String Save { get; set; }

     public PageReference save() {
     insert emp;
     PageReference pref = new ApexPages.StandardController(emp).view();
     return pref;       
     }

    public PageReference SN() {

    }
   }
3

In save and new method

First insert record and then redirect to new edit page

public PageReference SaveNew() {
    insert emp; //insert employee
    String strUrl = '/'+Employee__c.sobjecttype.getDescribe().getKeyPrefix()+'/e' ; 
    PageReference pref = new PageReference(strUrl);
    return pref;

}

Use describe call to get the prefix of sobject.. and prepare url for new page


Updates

IF you are redirecting to same VF page then use setRedirect to true

public PageReference SaveNew() {
        insert emp; //insert employee

        PageReference pref = Page.EmployeeCustomEdit;
        pref.setRedirect(true);//add this line 
        return pref;

    }

setRedirect(redirect)

  • Sets the value of the PageReference object's redirect attribute. If set to true, a redirect is performed through a client side redirect.
| improve this answer | |
  • 1
    @lathish you are welcome,.. – Ratan Paul Apr 12 '16 at 6:42
1

Suppose your VF page for this supports new record and suppose the name of the page is EmployeeCustomEdit.

public PageReference SN() {
    insert emp;
    PageReference pref = Page.EmployeeCustomEdit;
    return pref;  
}
| improve this answer | |
  • thank lance...record saving but new record creating function not working...actually my task is "Create employee record with values entered and redirect to same VF page so that user can create another employee". – lathish Apr 12 '16 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.