You need to sign in to do that
Don't have an account?
deeput05
Too many DML statements: 151
Hi,
i am trying insert more than 150 records and i unable do it since dml operation wont allow more tha 150 statments can i have any alternate method to achive this.
code:
public class insst1
{
public void sav()
{
for(integer i=0;i<250;i++)
{
Lead c=new lead();
c.FirstName='ffgghm'+i;
c.LastName='ffe';
c.email='ffl@gmail.com';
c.company='fffff';
insert c;
}
}
}
Hi,
Whenever you want to insert records that are more than one, you need to add them to a list and then perform insert on the list. So one dml helps you insert bulk records.So never have any DML statement inside a for loop.
So in your case, create a new List<Lead>, add each of those records in that list and then insert it.
public class insst1
{
public void sav()
{
List<Lead> lstOfLeadsToBeInserted = new List<Lead>();
for(integer i=0;i<250;i++)
{
Lead c=new lead();
c.FirstName='ffgghm'+i;
c.LastName='ffe';
c.email='ffl@gmail.com';
c.company='fffff';
lstOfLeadsToBeInserted.add(c);
}
insert lstOfLeadsToBeInserted;
}
}
All Answers
Hi,
Whenever you want to insert records that are more than one, you need to add them to a list and then perform insert on the list. So one dml helps you insert bulk records.So never have any DML statement inside a for loop.
So in your case, create a new List<Lead>, add each of those records in that list and then insert it.
public class insst1
{
public void sav()
{
List<Lead> lstOfLeadsToBeInserted = new List<Lead>();
for(integer i=0;i<250;i++)
{
Lead c=new lead();
c.FirstName='ffgghm'+i;
c.LastName='ffe';
c.email='ffl@gmail.com';
c.company='fffff';
lstOfLeadsToBeInserted.add(c);
}
insert lstOfLeadsToBeInserted;
}
}
Hi,
You can not perform more then 150 DML statements in one execution so what you need to do is , you need to create a list of all the records and insert that list using the DML statement, in this way you can insert multiple records only in one DML statement.
code:
public class insst1 {
public void sav() {
List<Lead> myLeadList = new List<Lead>();
for(integer i=0;i<250;i++) {
Lead c=new lead();
c.FirstName='ffgghm'+i;
c.LastName='ffe';
c.email='ffl@gmail.com';
c.company='fffff';
myLeadList.add(c);
}
if(myLeadList.size() > 0)
insert myLeadList;
}
}
so in above code example you are inserting 250 lead recoards but it will be considered as only one DML statement.
Thank You!