2

I want to mass update my account record. But I got this error when trying to deploy the code.

public class Insert_CustID_Old {

    public void insertCustId() {
        List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c FROM Account WHERE Cust_ID_SUJ__c != NULL];
        for(Account acc : accList){
            Account accNew = new Account();
            accNew.Id = acc.id;
            accNew.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;
            Update accNew;
        }
    }
}

System.LimitException: Too many DML statements: 151

4

4 Answers 4

8

In a single transaction, we have a limit of 150 DML

so if your SOQL query returns 151 records then for loop iterate over 151 time and Update accNew; will execute 151 times means you are doing 151 time DML operation that's why this error occurred.

To resolve this issue put this record into a list and update the list ex.

List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c FROM Account WHERE Cust_ID_SUJ__c != NULL];

List<Account> accListToUpdate = new List<Account>();
    for (Account acc : accList){
        Account accNew = new Account();
        accNew.Id = acc.id;
        accNew.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;
        accListToUpdate.add(accNew);
    }
update accListToUpdate; //perform update outside the loop so only single dml required. 

Another way

Since you are updating the list then you could do like this.

List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c FROM Account WHERE Cust_ID_SUJ__c != NULL];

    for (Account acc : accList){
        //this will update the list only. 
        acc.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;
    }
update accList ; //perform update outside the loop so only single dml required. 
3

Use DML statement (update) outside of for loop.

Secondly, you dont need to create separate account instance for updating. You should update the list which has been retrieved via SOQL query.

public class Insert_CustID_Old {

    public void insertCustId() {
        List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c FROM Account WHERE Cust_ID_SUJ__c != NULL];
        for (Account acc : accList){
            accNew.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;         
        }
        update accList;
    }
}
1

Don't use dml opertation in for loop it is not best practice. use below code you can avoid this error.

 public class Insert_CustID_Old {

public void insertCustId() {
List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c FROM Account WHERE Cust_ID_SUJ__c != NULL];
List<Account> updateList = new updateList();
for (Account acc : accList){
    Account accNew = new Account();
    accNew.Id = acc.id;
    accNew.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;
    updateList.add(accNew);
  }
   if(updateList.size()>0 && updateList !=null){
       update updateList;

  }
 }
1

Whenever you want to insert more than 1 record, you need to add records into a list and then perform a bulk DML. This will help you to avoid governor limits.

You can Update your code as below. Declare a list and inside for loop you can add records to that list and then make a dml call outside for loop.

public class Insert_CustID_Old {

 public void insertCustId() {

 //create a list to update account records in bulk
 List<Account> accountsToUpdate = new List<Account>(); 

 List<Account> accList = [SELECT Id, Cust_ID_SUJ__c, Cust_ID_SUJ_EBS_Old__c 
 FROM Account WHERE Cust_ID_SUJ__c != NULL];
 for (Account acc : accList){
     Account accNew = new Account();
     accNew.Id = acc.id;
     accNew.Cust_ID_SUJ_EBS_Old__c = acc.Cust_ID_SUJ__c;
     accountsToUpdate.add(accNew); // add the record to the list 
   }
  Update accountsToUpdate; //perform a bulk DML
 }

Refer this links to get more Idea:

Manipulate records with bulk DML

Solving Too Many DML Statements Error

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .