Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.
Challenge Description:

 

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:

 

 

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);

}

}

}

What am I doing wrong?

 

 
35 件の回答
  1. 2019年8月13日 18:56
    This worked for me. 

     

    public class AccountHandler {

     

        public static void insertAccount(Integer numberofAccounts){

     

            Integer n = 1;

     

            List<Account> addAccounts=new List<Account>();

     

            while(n <= numberofAccounts){

     

                

     

              

     

                Account tempaccount=new Account();

     

                tempaccount.Name='Acme Inc '+ n;

     

                tempaccount.AccountNumber='A000'+ n;

     

                addAccounts.add(tempaccount);

     

                

     

                n=n+1;

     

            }

     

            insert addAccounts;

     

            

     

        }

     

    }
  2. 2019年6月4日 16:17
    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;

    }

    }

     

     
  3. 2020年6月19日 17:35

    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;

    }

    }

     

    Save the Apex Class and open Execute Annonymous Window to execute the class.

     

    Enter Following lines of code ..

    AccountHandler.insertAccount(4);

     

    Click on Execute.

     

    The 3 ACME accounts will be inserted into the system.

     

     
  4. 2019年6月5日 19:17
    Thanks Tommaso )     

     

     ---  people don't forget to Call the method !   

     

    Class name -dot-  Method name (  argument in here to make 3 Account names  !!  )   

     

    >>             AccountHandler.insertAccount(3);
  5. 2020年3月1日 4:06
    First Create the 'Apex Class'

     

    <Solution 1 Provided by Tommaso Bolis>

     

    Then Open the 'Developer Console' and Under the 'Debug Menu' 

     

    Select 'Open Execute Anonymous Window'

     

    Finally, Input the Code:   AccountHandler.insertAccount(3);

     

    <Solution 2 Provided by Thomas Halden>First Create the 'Apex Class' <Solution 1 Provided by Tommaso Bolis> Then Open the 'Developer Console' and Under the 'Debug Menu' Select 'Open Execute Anonymous Window' Finally, Input the Code: Accoun
  6. 2022年1月19日 16:56

    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;

  7. 2020年4月1日 9:27
    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);

     

    I then had the error you guys got from not being able to find the 3 accounts.

     

    The last instruction is "Run your insertAccount method." The test doesn't run the method for you. You have to execute your method from the Execute Anonymous window in order to create the 3 accounts.

     

    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;

    }

    }

  8. 2019年10月24日 22:16
    Hey,

     

    What you are doing wrong is : instead of 

     

     insert(addAccounts);

     

    you should be writing

     

    insert addAccounts;

     

    (without the brackets)

     

    Sam
  9. 2019年6月5日 4:50
    You're welcome. Please select the best answer, it could be helpful for other users too. 
0/9000