0 votes
6 views
by

//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 Answer

0 votes
by
 
Best answer

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; 
}
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...