0

How can I retain default value in number field each time I add a row in table?

I just noticed that default value only shows once, but when I add new row, the number field is blank.

Note that I set the default value to 0.

Meanwhile, here are the controller and the vf page:

Controller:

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;  

        }
    }

}

VF Page:

<apex:page controller="creatingListOfRecordsController">
    <apex:form >

        <apex:pageBlock title="Creating List Of Account Records">
        <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/>
            </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!accountwrapperList}" var="page" id="table"> 
                    <apex:column headerValue="Name">
                        <apex:inputField value="{!page.account.name}"/>
                    </apex:column>
                    <apex:column headerValue="Phone">
                        <apex:inputField value="{!page.account.Phone}" />
                    </apex:column>
                    <apex:column headerValue="Sample Number">
                        <apex:inputField value="{!page.account.Sample__c}" />
                    </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>
1

In add row method this line update it

Accountwrapper actWrap = new Accountwrapper(new Account(Sample__c = 0)); 

So it will by default take 0. You can update it to any number.

1
  • hey, thanks a ton! I didn't know that it's that simple :)
    – lol
    Aug 30 '16 at 7:10

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.