I am running the statement in the Debug->Open Execute Anonymous Window option. I am trying to insert more than 150 Account records
List<Account> accounts = new List<Account>();
for(Integer i = 0; i < 200; i++)
{
Account account = new Account(Name = 'Test Account');
accounts.add(account);
}
insert accounts; // insert list - counts as 1 DML statement.
But i get the error:
System.LimitException: Too many DML statements: 151
Is there a way i can do this ?
There's nothing wrong with the code you posted, it's bulkified OK.
You need to examine the debug log carefully. You probably have a trigger on Account object (before insert, after insert etc) And something in there is written in suboptimal way. Creating data in a loop. A Task maybe? Updating something? The exact stack trace of where the limit was breached should help to nail it down.
If you don't have a trigger there's still way to run apex code for example from flow/process builder - but again, viewing the debug & stacktrace will help to nail it down.