function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deeput05deeput05 

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

}
}

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

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

vishal@forcevishal@force

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;

     }
}

This was selected as the best answer
Deshraj KumawatDeshraj Kumawat

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!