Salesforce Learning Guide

Tuesday, 18 November 2014

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 headerValue=”Account Number”>
<apex:inputField value=”{!acc.AccountNumber}”/>
<apex:column headerValue=”Account Type”>
<apex:inputField value=”{!acc.Type}”/>
<apex:column headerValue=”Industry”>
<apex:inputField value=”{!acc.Industry}”/>
<apex:pageBlockButtons >
<apex:commandButton value=”Add Accounts Row” action=”{!addAccount}”/>
<apex:commandButton value=”Save Accounts” action=”{!saveAccount}”/>

Controller related to above page:
public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }
public AddmultipleAccountsController()
listAccount=new list<Account>();
Public void addAccount()
Account acc = new Account();
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>

Output of above example:
Multiple records
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.
Multiple 3
This is simple example to explain how to enter multiple records at time form UI.


Post a Comment

Note: only a member of this blog may post a comment.