+ Start a Discussion
Che SFDCChe SFDC 

Need help with Bulkification of code

Dear all, below is my code which auto follows accounts when a new custom object KTT is created or updated. Code works fine on single record but I`m having trouble in bulkfying this code. I get "System.LimitException: Too many DML rows: 10001" error when I try to insert records in bulk via data loader. Is there something wrong with my code? I`m new to Apex world so it would really help if someone can point me to right direction.

Also, if I try to remove last few lines of DML statement, I get "System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []" error.
 
trigger RTTAutoFollow2 on RTT__C (after insert,after update) {


            List <EntitySubscription > NewFollowers = new List <EntitySubscription > ();   

                            List<Id> whatIds = new List<Id>();
               
   for(RTT__C sampletracker : trigger.new)

                         {
                whatIds.add(sampletracker.Account__c);
                             }
    
Map<Id, Account> Accounts = new Map<Id, Account>([SELECT Id, Name from Account WHERE Id in :whatIds]);
             

for(RTT__C sampletracker : trigger.new){  

              
                  if(sampletracker.Account__c != NULL){ 

 
           String Accid = sampletracker.Account__c;           
            String AccIds = accounts.get(Accid).ID;                  

    if (sampletracker.No_longer_Key__c == False)
{
  
               EntitySubscription ES = new EntitySubscription();    
               ES.SubscriberId = sampletracker.Assigned_Sales_Team__c;  
               ES.ParentId = sampletracker.Account__c;  
    
           }  
  
           try{  
                insert NewFollowers;                 
           }  
           catch (System.DmlException e){  
           }  
}
}}

 
Best Answer chosen by Che SFDC
Yogesh KulkarniYogesh Kulkarni
Hi Chetan,

Change the code around second for loop like.

for(RTT__C sampletracker : trigger.new){  
    if(sampletracker.Account__c != NULL){ 
        String Accid = sampletracker.Account__c;           
        String AccIds = accounts.get(Accid).ID;                  

        if (sampletracker.No_longer_Key__c == False)
        {
  
               EntitySubscription ES = new EntitySubscription();    
               ES.SubscriberId = sampletracker.Assigned_Sales_Team__c;  
               ES.ParentId = sampletracker.Account__c;  
               NewFollowers.add(ES);
    
        }  
}

try
{
    insert NewFollowers;
}
catch (System.DmlException e)
{  
}

All Answers

Yogesh KulkarniYogesh Kulkarni
Hi Chetan,

Change the code around second for loop like.

for(RTT__C sampletracker : trigger.new){  
    if(sampletracker.Account__c != NULL){ 
        String Accid = sampletracker.Account__c;           
        String AccIds = accounts.get(Accid).ID;                  

        if (sampletracker.No_longer_Key__c == False)
        {
  
               EntitySubscription ES = new EntitySubscription();    
               ES.SubscriberId = sampletracker.Assigned_Sales_Team__c;  
               ES.ParentId = sampletracker.Account__c;  
               NewFollowers.add(ES);
    
        }  
}

try
{
    insert NewFollowers;
}
catch (System.DmlException e)
{  
}
This was selected as the best answer
Che SFDCChe SFDC
Hi Yogest, thanks for your response. I tried but still getting same error. 
 
trigger RTTAutoFollow2 on RTT__C (after insert,after update) {

            List <EntitySubscription > NewFollowers = new List <EntitySubscription > ();   
                            List<Id> whatIds = new List<Id>();
                  for(RTT__C sampletracker : trigger.new)
                         {
                whatIds.add(sampletracker.Account__c);
                             }
    Map<Id, Account> Accounts = new Map<Id, Account>([SELECT Id, Name from Account WHERE Id in :whatIds]);

for(RTT__C sampletracker : trigger.new){  
    if(sampletracker.Account__c != NULL){ 
        String Accid = sampletracker.Account__c;           
        String AccIds = accounts.get(Accid).ID;                  

        if (sampletracker.No_longer_Key__c == False)
        {
  
               EntitySubscription ES = new EntitySubscription();    
               ES.SubscriberId = sampletracker.Assigned_Sales_Team__c;  
               ES.ParentId = sampletracker.Account__c;  
               NewFollowers.add(ES);
}  
}

try
{
    insert NewFollowers;
}
catch (System.DmlException e)
{  
}

 
Yogesh KulkarniYogesh Kulkarni
Hi Chetn,

Can you paste complete error message from debug log?

Thanks,
Yogesh
Che SFDCChe SFDC
Hi Yogesh,
You were right. I was an issue with "for" loop. If I finish my loop before try, system lets me upload bulk data. Thank you so much for your help!