Visualforce page to add multiple records
Adding multiple records:
Generally by using standard salesforce tab you can enter only one record at a time form UI. I don’t want to enter single record at a time. I want to enter multiple records at time. How can we achieve this? We can achieve this by using simple visual force page.
Example: I want to enter multiple accounts in single save. See the below visualforce page.
Visualforce page:
<apex:page Controller=”AddmultipleAccountsController”>
<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 Number”>
<apex:inputField value=”{!acc.AccountNumber}”/>
</apex:column>
<apex:column headerValue=”Account Type”>
<apex:inputField value=”{!acc.Type}”/>
</apex:column>
<apex:column headerValue=”Industry”>
<apex:inputField value=”{!acc.Industry}”/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value=”Add Accounts Row” action=”{!addAccount}”/>
<apex:commandButton value=”Save Accounts” action=”{!saveAccount}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
<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 Number”>
<apex:inputField value=”{!acc.AccountNumber}”/>
</apex:column>
<apex:column headerValue=”Account Type”>
<apex:inputField value=”{!acc.Type}”/>
</apex:column>
<apex:column headerValue=”Industry”>
<apex:inputField value=”{!acc.Industry}”/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value=”Add Accounts Row” action=”{!addAccount}”/>
<apex:commandButton value=”Save Accounts” action=”{!saveAccount}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller related to above page:
public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }
Account account = new Account();
public list<Account> listAccount{ get; set; }
public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}
{
listAccount=new list<Account>();
listAccount.add(account);
}
Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved;
}
}
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved;
}
}
Visualforce page Allaccountssaved( using this page above controller):
<apex:page sidebar=”false” showHeader=”true”>
<center><h3>Congrtas your accounts are successfully saved!!</h3></center>
</apex:page>
<center><h3>Congrtas your accounts are successfully saved!!</h3></center>
</apex:page>
Output of above example:
Here Add Account row is used to enter one more account and Save Account is used to save all account records you entered. See below screen, you can understand functionality of Add Account Row button.
After saving you will get below screen.
This is simple example to explain how to enter multiple records at time form UI.
Post a Comment