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
SFAdmin5SFAdmin5 

Is my trigger bulkified?

I've got a trigger on account object that populates a field that counts and sums up the number of related accounts to the account being updated/inserted/deleted.  Pretty straightforward.

 

This trigger works but I need to know whether it would be considered bulkified.  If it's not, any suggestions on what ought to be changed to bulkify it would be much appreciated.

 

Here it is:

 

trigger Account on Account (before insert, after insert, before update, after update) { 



// ---------------------------------------------------------------
 // Active Managed & Unmanaged Paying Customers processing
 // ------------------------------------------------------------------

    if ((Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete) && Trigger.isAfter) {    

         set<Id> AccountIds = new set<Id>();
 
             if(trigger.isInsert || trigger.isUpdate){
                 for(Account p : trigger.new){
                     AccountIds.add(p.Referring_Partner__c);
                 }
              }
 
             if(trigger.isDelete){
                for(Account  p : trigger.old){
                    AccountIds.add(p.Referring_Partner__c);
                 }
              }
 
          map<Id,Double> AccountMap1 = new map <Id,Double>();
 
              for(AggregateResult q : [select Referring_Partner__c,sum(Referring_Partner_Count__c)
                  from Account 
                  where Active_Products__c != null AND Managed_by_Partner__c !=null AND Referring_Partner__c IN :AccountIds 
                  group by Referring_Partner__c]){
                  
                  AccountMap1.put((Id)q.get('Referring_Partner__c'),(Double)q.get('expr0'));
               }
  
           map<Id,Double> AccountMap2 = new map <Id,Double>();
 
               for(AggregateResult q : [select Referring_Partner__c,sum(Referring_Partner_Count__c)
                   from Account where Active_Products__c != null AND Managed_by_Partner__c = false AND Referring_Partner__c IN :AccountIds 
                   group by Referring_Partner__c]){
          
                   AccountMap2.put((Id)q.get('Referring_Partner__c'),(Double)q.get('expr0'));
               }
 
           List <Account> AccountsToUpdate = new List<Account>();
 
               for(Account a : [Select Id, Active_Paying_M__c from Account where Id IN :AccountIds]){
   
                   Double Sum1 = AccountMap1.get(a.Id);
                   a.Active_Paying_M__c = Sum1 ;
                   Double Sum2 = AccountMap2.get(a.Id);
                   a.Active_Paying_U__c = Sum2;    
    
                   AccountsToUpdate.add(a);
                }
 
           
           update AccountsToUpdate ;
  
    }

}

 

Richie DRichie D

Hi.

 

Looks pretty good to me - no nested for loops with SOQL inside for example.

 

There is a slight 'win' where you can lose a select statement so you could change:-

List <Account> AccountsToUpdate = new List<Account>();
 
               for(Account a : [Select Id, Active_Paying_M__c from Account where Id IN :AccountIds]){
   
                   Double Sum1 = AccountMap1.get(a.Id);
                   a.Active_Paying_M__c = Sum1 ;
                   Double Sum2 = AccountMap2.get(a.Id);
                   a.Active_Paying_U__c = Sum2;    
    
                   AccountsToUpdate.add(a);
                }
 
           
           update AccountsToUpdate ;

 to:-

List <Account> AccountsToUpdate = new List<Account>();
 
               for(Id aId : AccountIds]){
   Account a = new Account(Id=a.Id);
                   Double Sum1 = AccountMap1.get(aId);
                   a.Active_Paying_M__c = Sum1 ;
                   Double Sum2 = AccountMap2.get(aId);
                   a.Active_Paying_U__c = Sum2;    
    
                   AccountsToUpdate.add(a);
                }
 
           
           update AccountsToUpdate ;

 A small change - only added this as didn't spot anything else ;)

 

Rich.

HariDineshHariDinesh

Hi,

 

Yes, Your trigger looking good and no change requried.

 

SFAdmin5SFAdmin5

Thanks for helping and taking a look guys

NickSmyrnosNickSmyrnos

You have logic to handle deletion events, but your trigger is not registered to this event and will not fire when a record is deleted.

SFAdmin5SFAdmin5

Good catch.  Yeah I forgot to update the trigger definition with "after delete".  Works now with that addition.  Thanks for catching that.