+ Start a Discussion
veena vaniveena vani 

I want a Wrapper class to display all the records and should save the selected records using save button

Hi Guys,

i have a vf page where 2 record types records are displaying with checkbox i should save those selected records .. can you please help me with this
here is my code i not wrote a wrapper class i am not understanding how to do it
public class custInterNeeds {
public PegasysKAM__Account_Plan__c accplan {set;get;}
public list <PegasysKAM__Customer_Need__c> cus {get;set;}
public list <PegasysKAM__Customer_Need__c> cus1 {get;set;}
public PegasysKAM__Customer_Need__c CusNeed {get;set;}
public string pid {get;set;}
public String currentRecordId {get;set;}
  
    public PageReference saveandnew() {
        return null;
    }
    public PageReference save() {
        return null ;
    }
    public PageReference Cancel(){
      PageReference pageRef = new PageReference('/'+'string pid');
       pageRef.setRedirect(true);
       return pageRef;
    }

public custInterNeeds(ApexPages.StandardController controller) {
    
string pid = ApexPages.currentPage().getParameters().get('pid');
     
         
  cus  = [SELECT  ID,Name, PegasysKAM__Account__c,PegasysKAM__Need__c,PegasysKAM__Status__c,PegasysKAM__Year__c,PegasysKAM__Details__c FROM PegasysKAM__Customer_Need__c
                  
                 where PegasysKAM__Account__c = :ApexPages.CurrentPage().getparameters().get('pid') AND RecordType.name='Customer' ];
                 
  cus1  = [SELECT  ID,Name, PegasysKAM__Account__c,PegasysKAM__Need__c,PegasysKAM__Status__c,PegasysKAM__Year__c,PegasysKAM__Details__c FROM PegasysKAM__Customer_Need__c
                  
                 where PegasysKAM__Account__c = :ApexPages.CurrentPage().getparameters().get('pid') AND RecordType.name='Internal' ];
 

    
}
}
<apex:page standardController="Account_Plan__c" Extensions="custInterNeeds" >


 <script type="text/avascript">
    function chkselected(obj,recid)
    {
        var inputcheckbox = document.getelementsbytagname("input");
        for(var i=0; i<inputcheckbox.length; i++){
                if(inputcheckbox[i].id.indexOf(recid)!=-1){
                    inputcheckbox[i].checked = obj.checked;
                }
            }
    }
    </script>
  <apex:form >
  <apex:pageblock title="Account Plan Edit" id="block" mode="edit" > 
  <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!save}" />
    <apex:commandButton value="Cancel" action="{!cancel}"/>
    <apex:commandButton value="Save&New" action="{!saveandnew}"/>
  </apex:pageBlockButtons>
  <apex:pageblocksection title="Information">
  <apex:outputField value="{!Account_Plan__c.Account__c}"/>
   <apex:inputField value="{!accplan.Key_Account_Manager__c}"/>
   <apex:inputField value="{!accplan.Start_Date__c}"/>
   <apex:inputField value="{!accplan.Status__c}"/>
   <apex:inputField value="{!accplan.End_Date__c}"/>
   <apex:inputField value="{!accplan.customer_approval__c}"/>
   <apex:inputField value="{!accplan.Customer_Stakeholder__c}"/>  
  </apex:pageblocksection>
  
   <apex:pageBlockSection title="Details">   
   <apex:inputField value="{!accplan.Overall_Objective__c}" style="width:120%;"/><br/>
   <apex:inputField value="{!accplan.Description__c}" style="width:150%;" />    
   </apex:pageBlockSection> 
     
   </apex:pageblock>
   
   <apex:pageBlock >
   
    
   <apex:pageblockSection title="Customer Needs" >
<!--<apex:outputPanel rendered="{!IF(<strong>PegasysKAM__Customer_Need__c</strong>.RecordType.Name == 'Customer')}">-->
 <!--  <apex:outputPanel rendered="{!Account_Plan__c.PegasysKAM__Customer_Need__c.RecordTypeId== '01I2v000003PVvj'}" > -->
   <apex:pageBlockSectionItem dataStyle="width:100%" labelStyle="width:100%" >
   <apex:pageblockTable value="{!cus}" var="c" styleclass="pageblock">
   
            <apex:column headerValue="Account" >
            <apex:outputField value="{!c.Account__c}" />
            </apex:column>
            <apex:column headerValue="Details">
            <apex:outputField value="{!c.Details__c}"/>
            </apex:column>
            <apex:column headerValue="Status">
            <apex:outputField value="{!c.Status__c}"/>
            </apex:column>
            <apex:column headerValue="Need">
            <apex:outputField value="{!c.Need__c}"/>
            </apex:column>
            <apex:column headerValue="Year">
            <apex:outputField value="{!c.Year__c}"/>
            </apex:column>
             
          <apex:column >
               <apex:facet name="header">
                    <apex:inputCheckbox onclick="chkselected(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!cus}" id="inputId"/>
                    </apex:column>       
        </apex:pageblockTable>
   </apex:pageBlockSectionItem>
<!-- </apex:outputpanel> -->
   </apex:pageblockSection> 
  
   <!-- section for internal needs-->
   
   
   <apex:pageblockSection title="Internal Needs" >
   <apex:pageBlockSectionItem dataStyle="width:100%" labelStyle="width:100%" >
   <apex:pageblockTable value="{!cus1}" var="c" styleclass="pageblock">
   
            <apex:column headerValue="Account" >
            <apex:outputField value="{!c.Account__c}" />
            </apex:column>
            <apex:column headerValue="Details">
            <apex:outputField value="{!c.Details__c}"/>
            </apex:column>
            <apex:column headerValue="Status">
            <apex:outputField value="{!c.Status__c}"/>
            </apex:column>
            <apex:column headerValue="Need">
            <apex:outputField value="{!c.Need__c}"/>
            </apex:column>
            <apex:column headerValue="Year">
            <apex:outputField value="{!c.Year__c}"/>
            </apex:column>
             
          <apex:column >
               <apex:facet name="header">
                    <apex:inputCheckbox onclick="chkselected(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!cus}" id="inputId"/>
                    </apex:column>       
        </apex:pageblockTable>
   </apex:pageBlockSectionItem>
   
   </apex:pageblockSection> 
  
   
   
  </apex:pageBlock>
  </apex:form>
</apex:page>
veena vaniveena vani
and then i  wrote wrapper class


public class custInterNeeds {
public PegasysKAM__Account_Plan__c accplan {get;set;}
public list <PegasysKAM__Customer_Need__c> cus {get;set;}
public list <PegasysKAM__Customer_Need__c> cus1 {get;set;}
public PegasysKAM__ActionPlan__c ActPlan {get;set;}
public string pid {get;set;}
public String currentRecordId{get;set;}
public list<wrapactionplan> wrapact {get; set;}
public list <PegasysKAM__Customer_Need__c> selectedCn {get;set;}
 
public custInterNeeds(ApexPages.StandardController controller) {
 currentRecordId= ApexPages.currentPage().getParameters().get('pid'); 
if(wrapact == null){
 wrapact =new list<wrapactionplan>();  
 for(PegasysKAM__Customer_Need__c pcn :[SELECT  ID,Name, PegasysKAM__Account__c,PegasysKAM__Need__c,PegasysKAM__Status__c,PegasysKAM__Year__c,PegasysKAM__Details__c FROM PegasysKAM__Customer_Need__c                
                 where PegasysKAM__Account__c = :currentRecordId] ){
                 wrapact.add(new wrapactionplan(pcn));
                 }
}
   
         //cus  = [SELECT  ID,Name, PegasysKAM__Account__c,PegasysKAM__Need__c,PegasysKAM__Status__c,PegasysKAM__Year__c,PegasysKAM__Details__c FROM PegasysKAM__Customer_Need__c                
           //      where PegasysKAM__Account__c = :currentRecordId AND RecordType.name='Customer' ];
                 
  //cus1  = [SELECT  ID,Name, PegasysKAM__Account__c,PegasysKAM__Need__c,PegasysKAM__Status__c,PegasysKAM__Year__c,PegasysKAM__Details__c FROM PegasysKAM__Customer_Need__c               
                // where PegasysKAM__Account__c = :currentRecordId AND RecordType.name='Internal' ]; 
  
   accplan = new PegasysKAM__Account_Plan__c();
    accplan.PegasysKAM__Account__c = currentRecordId;
}
public void processSelected(){
selectedCn = new list<PegasysKAM__Customer_Need__c>();
system.debug('cncncnc'+selectedCn );
for(wrapactionplan wc : wrapact){
if(wc.selected == true){
selectedCn.add(wc.pcn );
}
}
}
public PageReference Cancel(){
      return new PageReference('/'+currentRecordId);
    }
     
    public PageReference save() {    
insert accplan;
System.debug(accplan);
selectedCn = new list <PegasysKAM__Customer_Need__c>();
for(PegasysKAM__Customer_Need__c c : selectedCn ){
c.PegasysKAM__Account_Plan__c = accplan.id;
selectedCn.add(c);
}
system.debug('selectedcn'+selectedCn);
update selectedCn;
        return new PageReference('/'+accplan.id);
}

public PageReference OpenPage() {
insert accplan;
accplan = new PegasysKAM__Account_Plan__c();
    accplan.PegasysKAM__Account__c = currentRecordId;
    insert accplan;
      PageReference pr = new PageReference('/apex/ActionPlan');
      pr.setRedirect(True);
       return pr;      
    }
    public class wrapactionplan{
    public PegasysKAM__Customer_Need__c pcn {get; set;}
    public boolean selected {get; set;}
    public wrapactionplan (PegasysKAM__Customer_Need__c cn){
    pcn = cn;
    selected = false;
    }
    }        
}
ChinnoyChinnoy
public class wrapperClass1 {
    Public List<wrapAccountDisplay> lstWrap {get;set;}
    public List<Account> selectedAccounts {get;set;}
    public wrapperClass1(){
        if(lstWrap == null){
            lstWrap = new List<wrapAccountDisplay>();
            List<Account> acc = [Select id,Name,phone from Account limit 100];
            
            for(Account acco : acc){
                lstWrap.add(new wrapAccountDisplay(acco));
            }
        }
    }
    public void processSelected(){
        selectedAccounts = new List<Account>();
        for(wrapAccountDisplay wA : lstWrap){
            if(wA.selected == true){
                selectedAccounts.add(wA.acc);
            }
        }
    }
    Public class wrapAccountDisplay{
        public Account acc {get;set;} //
        public Boolean selected{get;set;}
                
        public wrapAccountDisplay(Account acco){
            acc = acco;
            selected = false;
        }
    }
}

add the Save functionality to it.