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
Ross Gilbert 31Ross Gilbert 31 

Bulk issue with trigger

I've got an after update trigger on account that works fine when individual records are updated.  But when I go to do a bulk update on say 200 accounts in my sandbox the trigger is not fired.  

Here's the class that the account after update trigger calls that does the work.  Something is not bulkified in here but I can't see it.  I don't think I've got any queries in for loops which usually is the problem.  What is wrong with this code?
 
public without sharing class RelatedAccountsManager{

    public static void createRelatedAccounts(object[] newTrigger) {

        List<Account> accList = (List<Account>) newTrigger;
            List<Account> accList3 = new List<Account>();
            List<Related_Account__c> raList = new List<Related_Account__c>();
            String thisDUNS;
            Id originalAccount;
                                
            for(Account a: accList){
                thisDUNS = a.Convey_Global_Ultimate_D_U_N_S__c;
                originalAccount = a.Id;
            }
         
            List<Related_Account__c> raListCheck = [SELECT Id, Related_Account__c, Account_Connection__r.Id from Related_Account__c WHERE Account_Connection__r.Id =: originalAccount LIMIT 1];
          
            if(raListCheck.size()>0){
            }else{                
              List<Account> accList2 = [SELECT ID,Type,Convey_Global_Ultimate_D_U_N_S__c FROM Account WHERE Convey_Global_Ultimate_D_U_N_S__c =: thisDUNS AND Convey_Global_Ultimate_D_U_N_S__c != null];        
                for(Account a1: accList2){
                    if(a1.Id != originalAccount && a1.Convey_Global_Ultimate_D_U_N_S__c!= null && accList2.size()>0 && a1.Type != 'Prospect'){
                        Related_Account__c ra = new Related_Account__c();
                        ra.Account_Connection__c = originalAccount;
                        ra.Related_Account__c = a1.Id;
                        raList.add(ra);       
                        accList3.add(a1);             
                     }
                }
             }
             insert raList;  
             
             List<Account> accList4 = new List<Account>();
             
             for(Account a2: accList3){
                accList4.add(a2);
             }
             
             update accList4;
     }   
}

 
RaidanRaidan
I think the problem is with these lines:

11            for(Account a: accList){
12                thisDUNS = a.Convey_Global_Ultimate_D_U_N_S__c;
13                originalAccount = a.Id;
14            }

It will only process one account record, instead of the whole batch. The DUNS and original value will get overwritten every time.

 
oleksandr vashchenkooleksandr vashchenko
Your code was designed like you work with only one record. 
List<Related_Account__c> raListCheck = [SELECT Id, Related_Account__c, Account_Connection__r.Id from Related_Account__c WHERE Account_Connection__r.Id =: originalAccount LIMIT 1];

Use something like this: 
Set<Id> accountIds = new Set<Id>()
for(Account a: accList){
            accountIds.add(a.Id);
        }
        
List<Related_Account__c> raListCheck = [SELECT Id, Related_Account__c, Account_Connection__r.Id from Related_Account__c WHERE Account_Connection__r.Id In : accountIds];