+ Start a Discussion
Swetha Sfdc1Swetha Sfdc1 

Add/Remove Functionality in a VF page for Creating Multiple Records For a Custom Object

Hi Friends,
Thanks in advance. I am new here and have an Problem with custom objects.
I want Add/Remove Functionality in a VF page for Creating Multiple Records For a Custom Object

Object Details:

Singular Label : Register
Plural Label     : Registers
Object Name   :Register
API Name   : Register__c

Below code represents Add/Remove Functionality in Accounts . I want below functionality in Customobject using (Register__c).
 
public with sharing class creatingListOfRecordsController {
    
    public list<Account> accountList{get;set;}
    public list<Accountwrapper> accountwrapperList{get;set;}
    public Integer counter{get;set;}
    
    public creatingListOfRecordsController(){
           counter = 0;
           accountList = new list<Account>(); 
           accountwrapperList = new list<Accountwrapper>();
           for(Integer i=0;i<5;i++){
               Accountwrapper actWrap = new Accountwrapper(new Account()); 
               counter++;
               actWrap.counterWrap = counter;
               accountwrapperList.add(actWrap); 
               
           }
       
    }
    
    public PageReference addRow(){
        //accountList.add(new Account());
        Accountwrapper actWrap = new Accountwrapper(new Account()); 
        
        counter++;
        actWrap.counterWrap = counter; 
        accountwrapperList.add(actWrap); 
        return null;    
    }
    public PageReference removingRow(){
    
        Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        
        for(Integer i=0;i<accountwrapperList.size();i++){
            if(accountwrapperList[i].counterWrap == param ){
                accountwrapperList.remove(i);     
            }
        }
        
        
        counter--;
        return null;    
    }
    
    public PageReference saving(){
        list<Account> updateAccountList;
        updateAccountList = new list<Account>();
        if(!accountwrapperList.isEmpty()){
            for(Accountwrapper accountWrapper:accountwrapperList){
                updateAccountList.add(accountWrapper.account);
            }
        }
        if(!updateAccountList.isEmpty()){
            upsert updateAccountList;
        }
       ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
       ApexPages.addMessage(myMsg); 
        return null;
    }
    
    public class Accountwrapper{
        public Account account{get;set;}
        public Integer counterWrap{get;set;}
        
        public Accountwrapper(Account act){
            this.account = act;  
             
        }
    }
    
}
I want same functionality for Custom Object  Sample output Link: (http://salesforceworld4u.blogspot.in/2014/01/addremove-functionality-in-vf-page-for.html)

Thanks & regards
Swetha
salesforce mesalesforce me
Hi Swetha can u check this code once....

<apex:page controller="creatingListOfRecordsController">
    <apex:form >
    
        <apex:pageBlock title="Creating List Of RegisterRecords">
        <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/>
            </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!RegisterwrapperList}" var="page" id="table"> 
                    <apex:column headerValue="Name">
                        <apex:inputField value="{!page.Register.name}"/>
                    </apex:column>
                    <apex:column headerValue="Phone">
                        <apex:inputField value="{!page.Register.Phone}" />
                    </apex:column>
                    <apex:column headerValue="Action">
                        <apex:commandLink value="Delete" action="{!removingRow}" immediate="true">
                            <apex:param name="index" value="{!page.counterWrap}"/>  
                        </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:commandButton value="Save" action="{!saving}" />
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

public with sharing class creatingListOfRecordsController {
    
    //public list<Register> RegisterList{get;set;}
    public list<Registerwrapper> RegisterwrapperList{get;set;}
    public Integer counter{get;set;}
    
    public creatingListOfRecordsController(){
           counter = 0;
           //RegisterList = new list<Register>(); 
           RegisterwrapperList = new list<Registerwrapper>();
           for(Integer i=0;i<5;i++){
               //Registerwrapper actWrap = new Registerwrapper(new Register()); 
               counter++;
               actWrap.counterWrap = counter;
               RegisterwrapperList.add(actWrap); 
               
           }
       
    }
    
    public PageReference addRow(){
        //RegisterList.add(new Register());
        //Registerwrapper actWrap = new Registerwrapper(new Register()); 
        
        counter++;
        actWrap.counterWrap = counter; 
        RegisterwrapperList.add(actWrap); 
        return null;    
    }
    public PageReference removingRow(){
    
        Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        
        for(Integer i=0;i<RegisterwrapperList.size();i++){
            if(RegisterwrapperList[i].counterWrap == param ){
                RegisterwrapperList.remove(i);     
            }
        }
        
        
        counter--;
        return null;    
    }
    
    public PageReference saving(){
        //list<Register> updateRegisterList;
        //updateRegisterList = new list<Register>();
        if(!RegisterwrapperList.isEmpty()){
            for(Registerwrapper RegisterWrapper:RegisterwrapperList){
                updateRegisterList.add(RegisterWrapper.Register);
            }
        }
        if(!updateRegisterList.isEmpty()){
            upsert updateRegisterList;
        }
       ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
       ApexPages.addMessage(myMsg); 
        return null;
    }
    
    public class Registerwrapper{
        //public Register Register{get;set;}
        public Integer counterWrap{get;set;}
        
       // public Registerwrapper(Register act){
            //this.Register = act;  
             
        }
    }
    
//}
salesforce mesalesforce me
hi swetha u can write the custom object Regester__c
Swetha Sfdc1Swetha Sfdc1
Hi , salesforce me Thanks for response. when I uploaded the code Apex class raising an Compile time error like below screen.
Error

Thanks & regards
Swetha