0

I am trying to run the following piece of code to update all Accounts without a primary contact. This method will look through all contacts of an account and assign the first contact it reads to be the primary contact of the account.

I am running into a System.LimitException: Too Many DML Statements: 151 error for exceeding 151 DML operations. Does anybody have any suggestions as to how I can resolve this?

public class setPrimaryContact {

public static void setPrimary(){

    boolean assigned = false;

    List<Account> accountList = [
        SELECT Name, Primary_Contact__c, (select id, Lastname, Type__C from contacts)
        FROM Account
    ];

    //Loop through all accounts
    for(Account acc:accountList){
        assigned = false;
        //Check if the account has contacts
        if(acc.Contacts.size()>0){
            //Check if the account already has a primary contact
            if(acc.Primary_Contact__c == null){
            //Loop through each contact
                for(Contact c:acc.Contacts){
                    //Check to see if the contact is a primary contact
                    if(!assigned && (c.Type__c == 'Primary Contact')){
                        //Assign that primary contact to be the Primary_Contact__c of the account
                        acc.Primary_Contact__c = c.Id;
                        assigned = true;
                        update acc;
                    }
                }
            } else {
                System.debug(acc.Name+' already has a primary contact .');
            }

            if(!assigned){
                System.debug(acc.Name+' has no primary contacts.');
            }
        } else {
            System.debug(acc.Name+' has no contacts.');
        } 
    }
}

}

  • 1
    Have you looked into bulkifying your code? – Nick Cook Jan 29 at 23:51
  • I've looked around at it looks like others are making a list of Accounts to update. Then at the end of the loop, they update the List of Accounts which are to be updated. Are you referring to this @NickCook? – Valkarth Jan 30 at 0:05
1

You have DML (database operations) inside a loop. That pattern is not going to work on the Salesforce platform.

What you must do: create and initialize a List variable i.e. List<Account> myListToUpdate = new List<Account>(); before you enter your outermost Account loop. Once you do your magic and set your fields, add each Account record that is to be updated to your List. Do NOT perform DML inside your loops. Remove the update acc; line from your code altogether, and replace it with myListToUpdate.add(acc);

Once you exit all of your loops, update your List variable.

Finally... here is some simple documentation to get you started learning to "think bulkified". https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_dml

  • there are other issues with your code as well. Learn the "SOQL for loop" pattern, that might help you if heap utilization becomes an issue. – krigi Jan 30 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.