1

Error

AddAccountTeamMember: System.LimitException: Too many DML rows: 10001

Code

public class AccountTeamMemberExample{
    public static void accountTeamAssignInsert(List<Account> newList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;

        for (Account acc : newList) {
            uid = acc.ownerId;
            for(CustomSettings__c s : CustomSettings__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where username in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(CustomSettings s : CustomSettings__c.getAll().values()){
                    for(User u : usr){
                    system.debug('Acc.ownerId:::'+Acc.ownerId);
                        AccountTeamMember atm = new AccountTeamMember();
                        AccountShare  nas = new AccountShare();
                        atm.AccountId = acc.id;                
                        atm.TeamMemberRole = s.TeamMemberRole__c;
                        atm.UserId= u.id; 
                        latm.add(atm);

                       if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){

                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);

                        }

                    }    
                }
            }

            try{

                insert(las);
                insert(latm);
            }catch(DMLException e){

                    system.debug('error on insert team members ' + e.getMessage());
            }  

    }
    //===UPDATE===
    public static void accountTeamAssignUpdate(List<Account> newList,List<Account> oldList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;
        String oldterritory_id;
        String newterritory_id;
        for(Account a : oldList){
            oldterritory_id = a.Territory_ID__c;
        }
        for(Account a : newList){
            newterritory_id = a.Territory_ID__c;
        }
        if(oldterritory_id != newterritory_id){
            for (Account acc : newList) {
                setids.add(acc.id);
            }

            List<AccountTeamMember> oldatm = [select id from AccountTeamMember where AccountId in : setids];
            if(oldatm != null){
                delete(oldatm);
            }
        }
        for (Account acc : newList) {
            uid = acc.ownerId;
            for(CustomSettings__c s : CustomSettings__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where username in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(CustomSettings__c s : CustomSettings__c.getAll().values()){
                    for(User u : usr){
                    system.debug('Acc.ownerId:::'+Acc.ownerId);
                        AccountTeamMember atm = new AccountTeamMember();
                        AccountShare  nas = new AccountShare();
                        atm.AccountId = acc.id;                
                        atm.TeamMemberRole = s.TeamMemberRole__c;
                        atm.UserId= u.id; 
                        latm.add(atm);

                       if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){

                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);

                        }

                    }    
                }
            }

            try{
                insert(las);
                insert(latm);                      
            }catch(DMLException e){
                    system.debug('error on insert team members ' + e.getMessage());
            }  

    }
}

and Trigger is:

trigger AccountTeam on Account (after insert,after update) {
    if(Trigger.isInsert){
        AccountTeamMemberExample.accountTeamAssignInsert(trigger.new);
    }
    if(Trigger.isUpdate){
        AccountTeamMemberExample.accountTeamAssignUpdate(trigger.new,trigger.old);
    }
}

Turned the Trigger off and records loaded as expected. I turned the Trigger back on and then tried an Update operation and that too failed with the above-referenced error, so it seems that there is still a problem with the bulk operations and the Trigger.

  • What line is the error happening on? – BarCotter Jan 20 '15 at 14:42
  • The two lines insert(las); insert(latm); are inserting too many rows in the database – Mihai Neagoe Jan 20 '15 at 14:48
2

This issue is most likely caused from your nested for loops, shown below. This pattern will cause the number of records in the las List to grow exponentially as the amount of Accounts, Users and Custom settings grow.

for(Account acc : newList){
    for(CustomSettings__c s : CustomSettings__c.getAll().values()){
        for(User u : usr){
            ...
            AccountShare  nas = new AccountShare();
            ...
            las.add(nas);

For example, if you inserted 20 Accounts, had 25 Custom Settings and 30 Users then you would end up trying to insert 15,000 (20 x 25 x 30) AccountShare records. Add some debugging on las and latm to see how the size of them grow.

If you truly need to create that many sharing objects then you may need to consider doing it using Apex Batch instead.

-2

below is the link that can help you out. https://developer.salesforce.com/forums/ForumsMain?id=906F0000000A61AIAS

  • Welcome to salesforce.stackexchange Prashant. It's not a good idea to limit your answr to a reference elsewhere, the content may dissapear over time or it may not be clear to what exactly your reference truly is. Please consider that an answer is mostly meaningful if you are able to offer an explanation, and do so directly here. Think of it as trying to collect all knowledge and answers on this site, rather than a huge library of URL's to other sites, google is already quite good at that :-). – Samuel De Rycke Jan 21 '15 at 10:28

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.