Skip to main content

public class AccountHandler {

    

public static void insertAccount(Integer newaccounts){

        Integer n = 1;

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

          

        while(n <= newaccounts){

           //create a new account

            Account store = new Account();

            store.Name = 'Acme Inc  ' + n;

            store.AccountNumber = 'A000' + n;

            addAccounts.add(store);

            

            //increment n

            n = n + 1;

            System.debug('n after incrementing: ' + n);

        }

         //insert all of the accounts in the list

        insert addAccounts;

    }

 

}

1 answer
  1. Feb 9, 2023, 10:00 PM

    Hi @Nagavelli Vydehi ,

     

    Try this

     

    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;

    }

    }

    https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A9637SAB

0/9000