+ Start a Discussion
Lorenz CortezLorenz Cortez 

Custom controller converts to Standard Controller with extention

Hello Everyone,

I should like to convert my Custom Controller to a Standard Controller with an extention so that I can refer my Visualforce Page to a related list button.Please help me out Thank you very much. My code is down below.

VF Page:
 
<apex:page controller="ManageListController" tabstyle="Employee_Skills__c">
 <apex:form id="emp_form">
   <apex:pageBlock title="Create Employee Skill">
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
        
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:inputField value="{!wrapper.empski.Skill_ID__c}"/> 
            <apex:actionSupport event="onchange" rerender="pageBlock" status="channelStatus"/>
         </apex:column>
          <apex:column headerValue="Proficiency">
            <apex:inputField value="{!wrapper.empski.Proficiency__c}"/> 
         </apex:column>
         <apex:column headerValue="Skill Type">
            <apex:inputField value="{!wrapper.empski.Skill_Type__c}"/> 
         </apex:column>
         <apex:column headerValue="Years of Experience">
            <apex:inputField value="{!wrapper.empski.Year_of_Experience__c}"/>
         </apex:column>
       
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" style="Button" action="{!delWrapper}" reRender="wtable" immediate="true">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
         
      </apex:pageBlockTable>
      
       <apex:commandButton value="Add Row" style="Button" action="{!addRows}" reRender="wtable" immediate="true" >
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
      
   </apex:pageBlock>
 </apex:form>
</apex:page>


My Custom Controller(That I would like to convert to a extension):

public class ManageListController {
 
     public List<EmpSkillWrapper> wrappers {get; set;}
     public static Integer toDelIdent {get; set;}
     public static Integer addCount {get; set;}
     private Integer nextIdent=0;
     
      public PageReference pageRef;
      public Employee_Skills__c getEmp;
     public Employee_Skills__c empi {set; get;}
     
     
     public ManageListController(ApexPages.StandardController controller) {
            
             this.getEmp = (Employee_Skills__c) controller.getRecord();
             this.empi = new Employee_Skills__c();
             wrappers = new List<EmpSkillWrapper>();
    }
     
     
     public ManageListController()
     {
          wrappers=new List<EmpSkillWrapper>();
          for (Integer idx=0; idx<1; idx++)
          {
           wrappers.add(new EmpSkillWrapper(nextIdent++));
          }
     }
      
   
     public class EmpSkillWrapper{
          public Employee_Skills__c empski {get;set;}
          public Integer ident {get;set;}
           
          public EmpSkillWrapper(Integer inIdent){
               ident=inIdent;
              empski=new Employee_Skills__c();
          }
     }
    
    public PageReference save(){
          
          List<Employee_Skills__c> eskills=new List<Employee_Skills__c>();
          for (EmpSkillWrapper wrap : wrappers){
               eskills.add(wrap.empski);
          }
           
          insert eskills;
           
          return new PageReference('/' + Schema.getGlobalDescribe().get('Employee_Skills__c').getDescribe().getKeyPrefix() + '/o');
          return null;
        // return new PageReference('/' + empski.Employee_ID__c);
         // return null;
    
     }
    
    public void addRows(){
          
          for (Integer idx=0; idx<addCount; idx++){
           wrappers.add(new EmpSkillWrapper(nextIdent++));
          }
         
     }
    
      public void delWrapper(){
          
          Integer toDelPos=-1;
          for (Integer idx=0; idx<wrappers.size(); idx++){
               if (wrappers[idx].ident==toDelIdent){
                    toDelPos=idx;
               }
          }
       
            if (-1!=toDelPos)
              {
                  if(wrappers.size() !=1){
                       wrappers.remove(toDelPos);
                       
                       }
              }
     }
      
      

     
}

Thank you in advance.
Abhilash Mishra 13Abhilash Mishra 13
you dont need to do anything , Just add standardcontroller  amd extension atrribute in page. and standard controller constructor in apex class.

try this,
 
<apex:page standardcontroller="Employee_skills__C"  extensions="ManageListController" tabstyle="Employee_Skills__c">
 <apex:form id="emp_form">
   <apex:pageBlock title="Create Employee Skill">
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
        
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:inputField value="{!wrapper.empski.Skill_ID__c}"/> 
            <apex:actionSupport event="onchange" rerender="pageBlock" status="channelStatus"/>
         </apex:column>
          <apex:column headerValue="Proficiency">
            <apex:inputField value="{!wrapper.empski.Proficiency__c}"/> 
         </apex:column>
         <apex:column headerValue="Skill Type">
            <apex:inputField value="{!wrapper.empski.Skill_Type__c}"/> 
         </apex:column>
         <apex:column headerValue="Years of Experience">
            <apex:inputField value="{!wrapper.empski.Year_of_Experience__c}"/>
         </apex:column>
       
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" style="Button" action="{!delWrapper}" reRender="wtable" immediate="true">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
         
      </apex:pageBlockTable>
      
       <apex:commandButton value="Add Row" style="Button" action="{!addRows}" reRender="wtable" immediate="true" >
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
      
   </apex:pageBlock>
 </apex:form>
</apex:page>
you have already defined the contrustor so no change here.
public class ManageListController {
 
     public List<EmpSkillWrapper> wrappers {get; set;}
     public static Integer toDelIdent {get; set;}
     public static Integer addCount {get; set;}
     private Integer nextIdent=0;
     
      public PageReference pageRef;
      public Employee_Skills__c getEmp;
     public Employee_Skills__c empi {set; get;}
     
     
     public ManageListController(ApexPages.StandardController controller) {
            
             this.getEmp = (Employee_Skills__c) controller.getRecord();
             this.empi = new Employee_Skills__c();
             wrappers = new List<EmpSkillWrapper>();
    }
     
     
     public ManageListController()
     {
          wrappers=new List<EmpSkillWrapper>();
          for (Integer idx=0; idx<1; idx++)
          {
           wrappers.add(new EmpSkillWrapper(nextIdent++));
          }
     }
      
   
     public class EmpSkillWrapper{
          public Employee_Skills__c empski {get;set;}
          public Integer ident {get;set;}
           
          public EmpSkillWrapper(Integer inIdent){
               ident=inIdent;
              empski=new Employee_Skills__c();
          }
     }
    
    public PageReference save(){
          
          List<Employee_Skills__c> eskills=new List<Employee_Skills__c>();
          for (EmpSkillWrapper wrap : wrappers){
               eskills.add(wrap.empski);
          }
           
          insert eskills;
           
          return new PageReference('/' + Schema.getGlobalDescribe().get('Employee_Skills__c').getDescribe().getKeyPrefix() + '/o');
          return null;
        // return new PageReference('/' + empski.Employee_ID__c);
         // return null;
    
     }
    
    public void addRows(){
          
          for (Integer idx=0; idx<addCount; idx++){
           wrappers.add(new EmpSkillWrapper(nextIdent++));
          }
         
     }
    
      public void delWrapper(){
          
          Integer toDelPos=-1;
          for (Integer idx=0; idx<wrappers.size(); idx++){
               if (wrappers[idx].ident==toDelIdent){
                    toDelPos=idx;
               }
          }
       
            if (-1!=toDelPos)
              {
                  if(wrappers.size() !=1){
                       wrappers.remove(toDelPos);
                       
                       }
              }
     }
      
      

     
}

Try this And let me know If you face any issue.
Please mark it solved by selecting a best answer, if it resolves the issue.

Thanks
Abhilash Mishra