+ Start a Discussion
Ravi sastriRavi sastri 

Visualforce page to replace standard new and edit buttons

Hello Developerss,
I have a requirement where I need to replace standard new and edit buttons on record page with VF page. Below is the code which I have got so far.
<apex:page standardcontroller="Lead" tabstyle="Lead" extensions="ExtensionClass">
 
 <apex:form>
 
 <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}"></apex:sectionheader>
 
 <apex:pageblock mode="edit" id="leadPB" title="Lead Edit">
 
 <apex:pageblockbuttons>
 <apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
 <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
 </apex:pageblockbuttons>
 
 <apex:pagemessages></apex:pagemessages> <!-- displays all message generated by the component -->
 
 <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
 <apex:inputField value="{!Lead.OwnerId}"/> 
 <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>

 <apex:pageblocksectionitem>
 <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
 <apex:outputpanel>
 <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
 <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
 </apex:outputpanel>
 </apex:pageblocksectionitem>
 <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
 
 <apex:inputField value="{!Lead.Company}" />
 <apex:inputfield value="{!Lead.Email}" required="true"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Status}"></apex:inputfield>

 <apex:inputfield value="{!Lead.Rating}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Industry}"></apex:inputfield>
 <apex:inputfield value="{!Lead.NumberOfEmployees}"></apex:inputfield>
     <apex:actionRegion >
         Target Sectors:  <apex:inputfield value="{!Lead.Target_Sectors__c}">
                                      <apex:actionSupport event="onchange" rerender="TargetSector,theMessage"/> 
                                      </apex:inputfield>
                                  </apex:actionRegion>
      <apex:outputPanel id="TargetSector">
                      <apex:pageBlockSection id="ProdFamily"  rendered="{!IF(Lead.Target_Sectors__c == 'Other', true, false)}"> 
                            <apex:inputfield value="{!Lead.Please_specify_the_Other_reason__c}"/>
                 </apex:pageBlockSection> 
    </apex:outputPanel> 
 </apex:pageblocksection>
 
 <apex:pageblocksection id="AddressInformationPBS" title="Address Information" columns="2">
 <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
  <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.City}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.State}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext> 
 </apex:pageblocksection>
 
 <apex:pageblocksection id="AdditionalInformationPBS" title="Additional Information">
 <apex:inputfield value="{!Lead.ProductInterest__c}"></apex:inputfield>
 <apex:inputfield value="{!Lead.CurrentGenerators__c}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.SICCode__c}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Primary__c}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.NumberofLocations__c}"></apex:inputfield>
 </apex:pageblocksection>
 
 <apex:pageblocksection id="DescriptionInformationPBS" title="DescriptionInformation">
 <apex:inputfield value="{!Lead.Description}"></apex:inputfield>
 </apex:pageblocksection>
 
 <apex:pageblocksection id="OptionPBS" title="Lead Information">
 <!-- If you want to have a checkbox to implement whether to use Active Assignment rules you will need to create your own using a custom Apex Extension and method -->
 </apex:pageblocksection>
 
 </apex:pageblock>
 
 </apex:form>
 
</apex:page>

Everything works fine here. I have replaced this vf page with standard New button and it works fine.
1) But, I do not know how to bind edit button with this page as it has to pre populate values which were entered while creating new record.
2) Save and Cancel buttons are working fine here. Can anyone help me with Save&New functionality with custom apex code.
3) In the above code, there is a field called "Target Sectors". It is enclosed under <apex:actionregion> tag. The name of this field is not populating on the page(Please test this code in your DE for better understanding).

Can anyone help me with above queries as I'm running out of time. Bieng a newbie, I couldnot able to catch what is the better solution for this. 
Thanks in advance
RS
pradeep kumar yadavpradeep kumar yadav
public PageReference saveNew() {
    
    try { 
     insert Lead; 
    } catch(System.DMLException e) {
        ApexPages.addMessages(e);
        return null;
    }   

    return (new ApexPages.StandardController(new Lead())).edit();        

}

You can write logic for save&new as mentioned above.

BUT , You cannot override custom Save&New button, but when you click save&new , your new and save button code will work behind that.
If you override the new and save button then the functionality of save&new also changes.
Ravi sastriRavi sastri
Hi Pradeep,
Thanks for your concern. When I put the code and add the extension name for my vf page, it is showing the following error
"Unknown constructor 'Extension.Extension(ApexPages.StandardController controller)'"
Can you help me with this..?

Thank you in advance
RS
pradeep kumar yadavpradeep kumar yadav
You have to define constructor when you use an extension.
public ApexPages.StandardSetController stdCntrlr {get; set;}

 public MyControllerExtension(ApexPages.StandardSetController controller) {

        stdCntrlr = controller;
    }

 
Ravi sastriRavi sastri
Hi Pradeep,
I've modified the code w.r.t your solution and even then, the error keeps repeating. Below is the code that you suggested along with my vf page.
Vf page:
<apex:page standardcontroller="Lead" tabstyle="Lead" extensions="MyControllerExtension">
 
 <apex:form >
  
 <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}"></apex:sectionheader>
 
 <apex:pageblock mode="edit" id="leadPB" title="Lead Edit">
 
 <apex:pageblockbuttons >
 <apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
            <apex:commandButton value="Save & New" action="{!saveNew}"/>
 <!-- If you wish to implement Save & New functionality you will have to write an Apex Extension with your own Save & New Method -->
 <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
 </apex:pageblockbuttons>
 
 <apex:pagemessages ></apex:pagemessages> <!-- displays all message generated by the component -->
 
 <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
 <!-- Make Owner field editable in system or else you won't be able to edit the Owner -->
 <apex:inputField value="{!Lead.OwnerId}"/> 
 <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
 
 <!-- Since we need to group two input fields together we need a pageBlockSectionItem with an Output panel.  We also needed to create a label so we know what field we are entering in -->
 <apex:pageblocksectionitem >
 <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
 <apex:outputpanel >
 <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
 <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
 </apex:outputpanel>
 </apex:pageblocksectionitem>
 <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
 
 <apex:inputField value="{!Lead.Company}" />
 <apex:inputfield value="{!Lead.Email}" required="true"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Status}"></apex:inputfield>

 <apex:inputfield value="{!Lead.Rating}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.Industry}"></apex:inputfield>
 <apex:inputfield value="{!Lead.NumberOfEmployees}"></apex:inputfield>
     <apex:actionRegion >
           <apex:inputfield value="{!Lead.Target_Sectors__c	}" id="Stage">
               <apex:outputtext value=""></apex:outputtext>
                                      <apex:actionSupport event="onchange" rerender="TargetSector,theMessage"/> 
                                      </apex:inputfield>
                                  </apex:actionRegion>
      <apex:outputPanel id="TargetSector">
                      <apex:pageBlockSection id="ProdFamily"  rendered="{!IF(Lead.Target_Sectors__c == 'Other', true, false)}"> 
                            <apex:inputfield value="{!Lead.Please_specify_the_Other_reason__c}"/>
                          <apex:outputtext value=""></apex:outputtext>
                 </apex:pageBlockSection> 
    </apex:outputPanel> 
 </apex:pageblocksection>
 
 <apex:pageblocksection id="AddressInformationPBS" title="Address Information" columns="2">
 <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
  <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.City}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.State}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext>
 
 <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
 <apex:outputtext value=""></apex:outputtext> <!-- Don't Really need to implement this extra space, but it's not bad practice -->
 </apex:pageblocksection>
 
 <apex:pageblocksection id="AdditionalInformationPBS" title="Additional Information">
 <apex:inputfield value="{!Lead.ProductInterest__c}"></apex:inputfield>
 <apex:inputfield value="{!Lead.CurrentGenerators__c}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.SICCode__c}"></apex:inputfield>
 <apex:inputfield value="{!Lead.Primary__c}"></apex:inputfield>
 
 <apex:inputfield value="{!Lead.NumberofLocations__c}"></apex:inputfield>
 </apex:pageblocksection>
 
 <apex:pageblocksection id="DescriptionInformationPBS" title="DescriptionInformation">
 <apex:inputfield value="{!Lead.Description}"></apex:inputfield>
 </apex:pageblocksection>
 
 <apex:pageblocksection id="OptionPBS" title="Optional">
 <!-- If you want to have a checkbox to implement whether to use Active Assignment rules you will need to create your own using a custom Apex Extension and method -->
 <apex:inputCheckbox label="Assign using active assignment rule"/>
 </apex:pageblocksection>
 
 </apex:pageblock>
 
 </apex:form>
 
</apex:page>

Extension Class:
public class MyControllerExtension {
    Lead Lead;
    public ApexPages.StandardSetController stdCntrlr {get; set;}

 public MyControllerExtension(ApexPages.StandardSetController controller) {

        stdCntrlr = controller;
    }
public PageReference saveNew() {
    
    try { 
     insert Lead; 
    } catch(System.DMLException e) {
        ApexPages.addMessages(e);
        return null;
    }   

    return (new ApexPages.StandardController(new Lead())).edit();        

}

}
The error is "Unknown constructor 'MyControllerExtension.MyControllerExtension(ApexPages.StandardController controller)'"
Can yu suggest me which part is to be edited/modified inorder to remove the error. 

Thanks in advance
RS
pradeep kumar yadavpradeep kumar yadav
StandardSetController 

It's wrong, change it to StandardController

We use StandardSetController for standard pagination
Ravi sastriRavi sastri
Hi Pradeep,
I've modified the code and when  I hit the save and new button, the following error is showing up
"
Error is in expression '{!saveNew}' in component <apex:commandButton> in page LeadForm: Class.MyControllerExtension.saveNew: line 12, column 1"
Can you help me with this?

Thanks in advance
RK