0

//My visualforce page code

<apex:page controller="AddMulAccountContr" >
    <apex:form >
    <apex:pageblock >
        <apex:pageblockTable value="{! listaccount}" var="acc">
           
            <apex:column headerValue="Account Name">
                    <apex:inputfield value="{!acc.Name}" />
             </apex:column>
             <apex:column headerValue="Account Name">
                       <apex:inputfield value="{!acc.Type}" />
                </apex:column>   
               <apex:column headerValue="Account Name">    
                          <apex:inputfield value="{!acc.Industry}" />
               </apex:column> 
           
        
        </apex:pageblockTable>
         <apex:pageBlockButtons >
                 <apex:commandbutton value="add multiple accounts" action="{!addmulrec}" />
                  <apex:commandbutton value="save account" action="{ !saveAccount}" />
        
            </apex:pageBlockButtons>
            
        
        </apex:pageblock>
    </apex:form>
</apex:page>

//My controller class

public class AddMulAccountContr {
    public  List<Account> listaccount{get;set;}
    Account a = new Account();
    public AddMulAccountContr(){
        
       listaccount = new List<Account>();
       listAccount.add(a) ;
        
    }
    public void addmulrec(){
        Account acc = new Account();
        listaccount.add(acc);
    }
    public PageReference saveAccount() {
              for(Integer i=0; i<listAccount.size(); i++)
              {
                 insert listaccount;
               }
                     return Page.pagerefdemo2;    
                       }
    

}

//my pagerefdEMO2 <apex:page >

        <h1>
          RECORDS INSERTED
        </h1>

</apex:page>
1
  • typo: action="{ !saveAccount}" should be action="{!saveAccount}" – cropredy Apr 12 at 19:28
1

I tried to run this snippet in dev org and I see that you are trying to loop in action attributes over the list of accounts while saving them.

To fix this, you can just simply ignore the loop while performing save action in your saveAccount() method in the AddMulAccountContr controller as below:

public class AddMulAccountContr {
public  List<Account> listaccount{get;set;}
Account a = new Account();
public AddMulAccountContr(){
    
    listaccount = new List<Account>();
    listAccount.add(a) ;
    
}
public void addmulrec(){
    Account acc = new Account();
    listaccount.add(acc);
}
public PageReference saveAccount() {
    
    insert listaccount;
    
    return Page.pagerefdemo2; 
}
}
1
  • Thank you so much...It works for me – Shivani Tanwar Apr 13 at 5:26
1

The reason for this error is you have a typo here (space between { and ! ):

action="{ !saveAccount}" 

should be

action="{!saveAccount}" 

This won't be detected by the VF compiler but appears as a runtime error when you click the button

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.