
Use DML to insert multiple records into a Salesforce database
Acme Inc. has opened three new locations in Texas. Acme needs to insert the new accounts into its database. Create a class and one method that creates three new accounts and adds them to the database.
- Create a public Apex class named AccountHandler
- Add a public static method to the class:
- Name: insertAccount
- Include a parameter for the number of new accounts:
- Data type: integer
- Create a list of Account records:
- List name: addAccounts
- Use a while loop to add three new Accounts to the list, where n is a value that is incremented by 1 during each iteration of the loop:
- Name: Acme Inc n
- AccountNumber: A000n
- Hint: You did something like this when you created three new Tea Factory stores.
- Write one DML statement that inserts all three records into the database at one time
- Run your insertAccount method.
Error Message:
We can't find code that creates an 'addAccounts' list, adds three accounts to the 'addAccounts' list, and then inserts the 'addAccounts' list.
Code:
What am I doing wrong?public class AccountHandler {
public static void insertAccount(Integer numAccts) {
List<Account> addAccounts = new List<Account>();
Integer counter = 1;
while(counter <= 3) {
Account a = new Account();
a.Name = 'Acme Inc ' + counter;
a.AccountNumber = 'A000' + counter;
addAccounts.add(a);
counter++;
}
if(addAccounts.size() > 0) {
Database.insert(addAccounts);
}
}
}
This worked for me. This works public class AccountHandler {
public static void insertAccount(Integer value) {
Integer counter = 1;
List<Account> addAccounts = new List<Account>();
while(counter <= value) {
System.debug('Counter Value before Incrementing ' + counter);
Account a = new Account();
a.Name = 'Acme Inc ' + counter;
a.AccountNumber = 'A000' + counter;
addAccounts.add(a);
counter = counter + 1;
}
insert addAccounts;
}
}
public class AccountHandler {
public static void insertAccount(Integer n)
{
List<Account> addAccounts =new List<Account>();
Integer num1 =1;
While(num1<n)
{
Account newacc = new Account();
//newacc.add(Name = 'Acme Inc n' +n, AccountNumber = 'A000n' +n);
newacc.Name = 'Acme Inc n' +n;
newacc.AccountNumber = 'A000n' +n;
addAccounts.add(newacc);
System.debug('acmeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee');
num1++;
}
insert addAccounts;
}
}
Enter Following lines of code ..
AccountHandler.insertAccount(4);
Thanks Tommaso ) First Create the 'Apex Class' This works for me: (this class was written before from someone in this post)
public class AccountHandler {
public static void insertAccount(Integer value) {
Integer counter = 1;
List<Account> addAccounts = new List<Account>();
while(counter <= value) {
System.debug('Counter Value before Incrementing ' + counter);
Account a = new Account();
a.Name = 'Acme Inc ' + counter;
a.AccountNumber = 'A000' + counter;
addAccounts.add(a);
counter = counter + 1;
}
insert addAccounts;
}
}
AFTER THAT in Debug (CTRL + E) i write this code.
Account acct = new Account(
Name='Acme Inc',
Phone='(415)555-8989',
NumberOfEmployees=50,
BillingCity='San Francisco');
insert acct;
Account acct2 = new Account(
Name='Acme Inc',
Phone='(415)555-8989',
NumberOfEmployees=50,
BillingCity='San Francisco');
insert acct2;
Account acct3 = new Account(
Name='Acme Inc',
Phone='(415)555-8989',
NumberOfEmployees=50,
BillingCity='San Francisco');
insert acct3;
Instructions say create a list, not an array. They also say use DML statement, not a database method. It doesn't like
Account[] addAccounts = new List<Account>();
and
database.insert(addAccounts);
My (finally) working code.
public class AccountHandler {
public static void insertAccount(Integer numOfAccts){
List<Account> addAccounts = new List<Account>();
Integer i = 1;
while(i <= numOfAccts){
addAccounts.add(new Account(Name = 'Acme Inc ' + i, AccountNumber = 'A000' + i));
i++;
}
insert addAccounts;
}
}
Hey, You're welcome. Please select the best answer, it could be helpful for other users too. Are you looking for Free virtual machine software? Here we are sharing about top 10 Free virtual machine software. Check now