Bob Buzzard Blog: Managing a list of New Records in Visualforce: This week's post concerns bulk creation of an unknown number of sobject records - Accounts, for example. In this situation I want to dis......
Below code is pasted from "http://bobbuzzard.blogspot.in/2011/07/managing-list-of-new-records-in.html" for explanation see the original link .....
<apex:page controller="ManageListController" tabstyle="Account">
<apex:form >
<apex:pageBlock title="Bulk Account Create">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
<apex:column headerValue="Ident">
<apex:outputText value="{!wrapper.ident}"/>
</apex:column>
<apex:column headerValue="Name">
<apex:inputField value="{!wrapper.acc.Name}"/>
</apex:column>
<apex:column headerValue="Parent">
<apex:inputField value="{!wrapper.acc.ParentId}"/>
</apex:column>
<apex:column headerValue="Industry">
<apex:inputField value="{!wrapper.acc.Industry}"/>
</apex:column>
<apex:column headerValue="Type">
<apex:inputField value="{!wrapper.acc.Type}"/>
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="5" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
/*********************************** controller class ********************/
Below code is pasted from "http://bobbuzzard.blogspot.in/2011/07/managing-list-of-new-records-in.html" for explanation see the original link .....
<apex:page controller="ManageListController" tabstyle="Account">
<apex:form >
<apex:pageBlock title="Bulk Account Create">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
<apex:column headerValue="Ident">
<apex:outputText value="{!wrapper.ident}"/>
</apex:column>
<apex:column headerValue="Name">
<apex:inputField value="{!wrapper.acc.Name}"/>
</apex:column>
<apex:column headerValue="Parent">
<apex:inputField value="{!wrapper.acc.ParentId}"/>
</apex:column>
<apex:column headerValue="Industry">
<apex:inputField value="{!wrapper.acc.Industry}"/>
</apex:column>
<apex:column headerValue="Type">
<apex:inputField value="{!wrapper.acc.Type}"/>
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="5" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
/*********************************** controller class ********************/
public class ManageListController { public List<AccountWrapper> wrappers {get; set;} public static Integer toDelIdent {get; set;} public static Integer addCount {get; set;} private Integer nextIdent=0; public ManageListController() { wrappers=new List<AccountWrapper>(); for (Integer idx=0; idx<5; idx++) { wrappers.add(new AccountWrapper(nextIdent++)); } } public void delWrapper() { Integer toDelPos=-1; for (Integer idx=0; idx<wrappers.size(); idx++) { if (wrappers[idx].ident==toDelIdent) { toDelPos=idx; } } if (-1!=toDelPos) { wrappers.remove(toDelPos); } } public void addRows() { for (Integer idx=0; idx<addCount; idx++) { wrappers.add(new AccountWrapper(nextIdent++)); } } public PageReference save() { List<Account> accs=new List<Account>(); for (AccountWrapper wrap : wrappers) { accs.add(wrap.acc); } insert accs; return new PageReference('/' + Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix() + '/o'); } public class AccountWrapper { public Account acc {get; private set;} public Integer ident {get; private set;} public AccountWrapper(Integer inIdent) { ident=inIdent; acc=new Account(Name='Bulk Acc ' + ident); } }}