1

I want to add a button "ADD NEW RECORD". When the button is clicked, a new row for input fields of accounts should be added along with the checkbox wrapped with it. How can this be done? EDIT: Also, is there a way by which only selected accounts get updated and not the whole data? Thanks in advance.

VF PAGE

<apex:page controller="wrapAccountAndCheckbox" tabStyle="Account" >

    <apex:form id="frm">
        <apex:pageBlock title="Account Records">
            <table>
                <tr>
                    <th> Name </th>            
                    <th> Fax </th>
                    <th> Phone </th>
                </tr>

                <apex:repeat value="{!wrapClassList}" var="accountTable" id="Acnt_Table">

                    <tr>
                        <td> <apex:inputField value="{!accountTable.actWrap.Name}"/> </td>
                        <td> <apex:inputField value="{!accountTable.actWrap.fax}"/> </td>
                        <td> <apex:inputField value="{!accountTable.actWrap.phone}"/> </td>
                        <td> <apex:inputCheckbox value="{!accountTable.flag}"/> </td>
                    </tr>



                </apex:repeat>
            </table>  


            <apex:pageBlockButtons >
                <apex:commandButton value="SAVE" action="{!save}" />
                 <apex:commandButton value="DELETE" action="{!deleteAcc}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER WITH WRAPPER CLASS

public class wrapAccountAndCheckbox {

    public List<wrapperClass> wrapClassList{get;set;}
    public List<Account> actList;
    public wrapperClass wc{get;set;}
    List<account> wcList=new List<account>();


    public wrapAccountAndCheckbox(){
        wrapClassList = new List<wrapperClass>();
        actList = new List<Account>();
        actList = [SELECT Id, Name, Fax, Phone FROM Account];
        For(Account ac : actList){
            wc = new wrapperClass();
            wc.flag = false;
            wc.actWrap = ac;
            wrapClassList.add(wc);
        }
    }
    public class wrapperClass{
        public boolean flag{get;set;}
        public Account actWrap{get;set;}


    }

    public void updateDetails()
    {
        if(wrapClassList!=null)
        {   wcList.clear();
            for(wrapperClass wc: wrapClassList)
            {   if(wc.flag==true)
            {wcList.add(wc.actWrap);
            }
            }        
        }
    }
    public pageReference save()
    {updateDetails();   
     update wcList;
     PageReference pageRef = new PageReference('https://cunning-badger-419701-dev-ed--c.ap4.visual.force.com/apex/AccountActions');
     return pageRef;
    }

    public pageReference deleteAcc()
    {
        updateDetails();
        try{
            for(Account a:actList)
            {   if(wcList.contains(a))
                delete a;}
        }
        catch(Exception e){
            system.debug('Exception in Delete>>>>'+e);
        }
        PageReference pageRef = new PageReference('https://cunning-badger-419701-dev-ed--c.ap4.visual.force.com/apex/AccountActions');
        return pageRef;
    }

}
4

This is a very common requirement. First add a Add row button with ypur table

<apex:commandButton value="Add More" action="{!addNewRow}" rerender="frm" Status="status" immediate="true" />

and then in your controller add row in existing list

public void addNewRow() {
    wrapperClass wc = new wrapperClass();
    wc.flag = false;
    wc.actWrap = new Account();
    wrapClassList.add(wc);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.