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
sanjay dubey 19sanjay dubey 19 

Need to do standard button "save and new" functionality using visualforce page and apex


<apex:page controller="V2Controller" tabStyle="Contact">
<apex:form >
<apex:sectionHeader subtitle="New Contact" title="Contact Edit" description="Contacts not associated with accounts are private and cannot be viewed by other users or included in reports." help="https://help.salesforce.com/articleView?err=1&id=contacts_overview.htm&siteLang=en_US&ty...>
<apex:pageBlock title="Edit Contact" >
   <apex:pageBlockSection title="Contact Information" columns="2" onkeydown="1" collapsible="false">
       <apex:outputField value="{!contact.ownerid} "/>
       <apex:inputField value="{!contact.Phone}"/>
      
    <apex:pageBlockSectionItem >
        <apex:outputlabel value="First Name"/>
            <apex:outputpanel >
            <apex:inputfield value="{!Contact.Salutation}"/>
            &nbsp;
            <apex:inputfield value="{!Contact.FirstName}"/>
        </apex:outputpanel>
   </apex:pageBlockSectionItem>
       <apex:inputField value="{!contact.homephone}" />
       <apex:inputField value="{!contact.LastName}" required="true"/>
       <apex:inputField value="{!contact.MobilePhone}"/>
       <apex:inputField value="{!contact.Accountid}"/>
        <apex:inputField value="{!contact.otherphone}"/>
        <apex:inputField value="{!contact.Title}"/>
        <apex:inputField value="{!contact.fax}"/>
        <apex:inputField value="{!contact.Department}"/>
        <apex:inputField value="{!contact.Email}"/>
        <apex:inputField value="{!contact.Birthdate}"/>
       <apex:inputField value="{!contact.Assistantname}"/>
       <apex:inputField value="{!Contact.ReportsToId}"/>
       <apex:inputField value="{!contact.AssistantPhone}"/>
       <apex:inputField value="{!contact.LeadSource}"/>
      
      
      
      
   </apex:pageBlockSection>
   <apex:pageBlockSection columns="2" title="Address Information" collapsible="false">
       <apex:inputField value="{!contact.MailingStreet}"/>
       <apex:inputField value="{!contact.otherStreet}"/>
        
       <apex:inputField value="{!contact.MailingCity}"/>
       <apex:inputField value="{!contact.otherCity}"/>
      
       <apex:inputField value="{!contact.MailingState}"/>
       <apex:inputField value="{!contact.otherState}"/>
      
       <apex:inputField value="{!contact.MailingPostalCode}"/>
       <apex:inputField value="{!contact.otherPostalCode}"/>
      
       <apex:inputField value="{!contact.MailingCountry}"/>
       <apex:inputField value="{!contact.OtherCountry}"/>
      
      
   </apex:pageBlockSection>
  
  
   <apex:pageBlockSection title=" Additional Information" collapsible="false">
      
       <apex:inputField value="{!contact.Languages__c}"/>
       <apex:inputField value="{!contact.Level__c}"/>
  
   </apex:pageBlockSection>
  
     <apex:pageBlockSection title=" Description Information" collapsible="false" >
      
       <apex:inputTextarea value="{!contact.Description}" rows="7" cols="90"/>
   </apex:pageBlockSection>
   <apex:pageBlockButtons >
       <apex:commandButton value="Save" Action="{!save}"/>  
       <apex:commandButton value="Save & New" Action="{!SaveAndNew}"/>
       <apex:commandButton value="Cancel" Action="{!cancel}" immediate="true"/> 
   </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>

My Controller class

public class V2Controller
{
    
  public Account acc=new account();
  public Contact contact{get;set;}
  
            public V2Controller()
            {
                acc=[select id,Billingcity,BillingStreet,BillingState,BillingCountry,BillingPostalCode,ShippingStreet,
                            ShippingCity,Shippingstate,ShippingPostalCode,Shippingcountry from account where id=:apexpages.currentpage().getparameters().get('id')];
                            
                    contact=new Contact();
                    contact.Mailingcity=acc.Billingcity;
                    contact.MailingStreet=acc.BillingStreet;
                    contact.MailingState=acc.BillingState;
                    contact.MailingPostalCode = acc.BillingPostalCode;
                    contact.Mailingcountry=acc.BillingCountry;
                    
                    contact.otherStreet = acc.ShippingStreet;
                    contact.otherCity= acc.ShippingCity;
                    contact.otherstate = acc.Shippingstate ;
                    contact.otherPostalCode = acc.ShippingPostalCode;
                    contact.othercountry = acc.Shippingcountry;  
                    contact.accountid=acc.id;
            }
            
            public PageReference save()
             {
                insert contact;
                 //   ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
                    PageReference pg = new PageReference('/'+acc.Id);
                    pg.setRedirect(true);
                    return pg;        
                }
          
            public pagereference cancel()
            {
                    PageReference pg = new PageReference('/'+acc.Id);
                    pg.setRedirect(true);
                    return pg;
              }
              
               public Pagereference SaveAndNew()
            {
                   insert contact;
                   return null;
              }

}


// i am trying  to create visualforce page which populate the value of account address field here values are populated byt my save and new button is not working fine please suggest 
Thanks in advance
sanjay
Best Answer chosen by sanjay dubey 19
Thiruchuri AdityanThiruchuri Adityan
Replace your saveAndNew method with the following code

public Pagereference SaveAndNew()
            {
                     insert contact;
                     PageReference pg = new PageReference('/003/e');
                    pg.setRedirect(true);
                    return pg;
              }

 

All Answers

Thiruchuri AdityanThiruchuri Adityan
Replace your saveAndNew method with the following code

public Pagereference SaveAndNew()
            {
                     insert contact;
                     PageReference pg = new PageReference('/003/e');
                    pg.setRedirect(true);
                    return pg;
              }

 
This was selected as the best answer
sanjay dubey 19sanjay dubey 19
Thanks Thiruchuri Adityan its working 
like that 

public pagereference SaveAndNew(){
    insert contact;
    PageReference pg = new PageReference('/apex/v2?id='+acc.id);
    pg.setRedirect(true);
    return pg;
}
sanjay dubey 19sanjay dubey 19
hi Thiruchuri Adityan
i am wirting test class for same but my pagereference method is not covered please suggest

@istest
public class v2Controller_test {
        static testMethod void myTest1(){ 
            
            
            
        Account acc1 = new account();            
            acc1.name = 'test12';
            acc1.Industry='technology';
            acc1.Billingcity='noida';
            acc1.BillingStreet='b34';
            acc1.BillingState='up';
            acc1.BillingPostalCode = '201301';
            acc1.BillingCountry   ='india' ;            
                acc1.ShippingStreet ='c10';
                acc1.ShippingCity = 'noida';
                acc1.Shippingstate = 'up';
                acc1.ShippingPostalCode = '201301';
                acc1.Shippingcountry = 'india';
            insert acc1;
        
       contact con = new contact();
            con.firstname='sam';
            con.Accountid= acc1.id;
            con.LastName='test89';
            con.Mailingcity = acc1.Billingcity;
            con.MailingStreet = acc1.BillingStreet;
            con.MailingState = acc1.BillingState;
            con.MailingPostalCode = acc1.BillingPostalCode;
            con.MailingCountry = acc1.BillingCountry;
            insert con;
  
           PageReference pageRef=Page.v2; 
           pageRef.getParameters().put('id', String.valueOf(+acc1.id));
           system.Test.setCurrentPage(pageRef);

       v2Controller v2con = new v2Controller();
         
       v2con.cancel();
       v2con.save();
       v2con.SaveAndNew();
    }
    
}

pagerefernce save() and saveAndnew() not covered please suggest 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]

Class.V3controller.save: line 28, column 1
Class.v3Controller_test.myTest1: line 50, column 1
thanks in advance
sanjay