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>
Controller related to above page:
public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }
public AddmultipleAccountsController() {
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;
}
}
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>
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 a simple example to explain how to enter multiple records at time form UI.
Hello,
I have implemented your logic but here is the problem:
If I fill out only one row, and save my form it does not save the record.
If I fill out one row and click the add more button to enter subsequent records, then all rows get saved except the last.
You need to click the add row button to save the record. Is there a way to save all the records entered by just clicking the Save account?
Thanks,
Use the following and see if it solves the problem, because size can’t be ‘0’. So the for loop starts from 1.
public PageReference saveAccount() {
for(Integer i=1; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved;
}
Hi,
why do we need for loop in saveAccount() method. We are inserting entire list so whats the purpose of for loop? we did not used variable i in the method
please write test class for this code
Hello,
This worked perfectly, thanks.
However I am having problems with a test class to get the controller into production . . . do you have any advice/examples.
Many Thanks
Error: AddmultipleAccountsController Compile Error: DML requires SObject or SObject list type: List at line 20 column 1
I need some help:
I implemented this and it works fine if I only add 2 records, if I try to add three, i get this error:
System.DmlException: Insert failed. First exception on row 0 with id a2p180000000jbnAAA; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression ‘{!saveTe}’ in component in page timeentry: Class.AddmultipleTimeEntriesController.saveTe: line 19, column 1
Class.AddmultipleTimeEntriesController.saveTe: line 19, column 1
Here is my controller:
public class AddmultipleTimeEntriesController {
Time_Tracking__c Te = new Time_Tracking__c();
public list listTimeEntries{ get; set; }
public AddmultipleTimeEntriesController()
{
listTimeEntries=new list();
}
Public void addTe()
{
Time_Tracking__c tte = new Time_tracking__c();
listTimeEntries.add(tte);
}
public PageReference saveTe() {
for(Integer i=0; i<listTimeEntries.size(); i++)
{
insert listTimeEntries;
}
return Page.AllTesaved;
}
}
Any help would be appreciated!!
Thanks
Sarah
Seems like you are inserting “ID” with your insertion operation. You should not give ID field during insertion. After insertion Salesforce will generate new ID for each record.
I am not passing the ID that i know if. If I change the insert to an upsert, it works. This does not make sense to me as they are not inserted when I click on add line. I posted my code. Any help would be appreciated.
I want create this functionality please help me .
1. account (input & output ) save then open output section this section as buttons ( EDIT & DELETE & CREATE CONTACT )
2. click on create contact button this type page input click on save
3. finally button submit click this button to create contacts and account.
please help me .
Hi , I am using same logic to create multiple COntacts . I click a CUstom Button which directs me to a VF page , and then I fill out contact details. After Save , Contact gets created but it is not associated to Account. Please find my Code below :-
VfPage :-
Controller ———————————————————————
public class AddMultipleContacts {
Id AcctID;
public List ContactList {get;set;}
public Integer rowNum{get;set;}
public AddMultipleContacts(){
AcctID = ApexPages.currentPage().getParameters().get(‘AcctID’);
ContactList = new List();
ContactList.add(new contact(AccountId = AcctID));
}
public void insertContacts(){
insert ContactList;
}
public void insertRow(){
ContactList.add(new contact());
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get(‘index’));
ContactList.remove(rowNum);
}
}
Could not resolve the entity from value binding ‘{!acc.Name}’. can only be used with SObjects, or objects that are Visualforce field component resolvable.
Hey nice blog by the way
Our Salesforce developers can assist you for Salesforce QuickBooks integration for both QuickBooks Desktop edition as well as QuickBooks Online edition. Salesforce is a powerful CRM that has most of the necessary objects required to store financial and eCommerce data like Accounts, Contacts, Quotes, Products, PriceBooks, Orders, Order Products etc
https://webnersolutions.com/salesforce/salesforce-quickbooks-integration/
Hey everyone,
CAn anyone help me update this script to add multiple Opportunity Products. I want this Visualforce page to run on a tab on the opportunity record and update that record with the added opportunity product.
Thanks