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
JAckerman09JAckerman09 

Any way to accomplish this goal with less SOQL queries? Will hit limits during mass updates.

I am running this trigger that automatically adds users as followers anytime they are in the lookup fields Portfolio Manager, Trust Oficer, Wealth Advisor, Other Team Member 1 or Other Team Member 2 on an Account record.

 

I am using SOQL queries to:

 

  • Check the records current followers to ensure that the user is not already following the record (which would cause a duplicate value error)
  • To make sure that the user is not following more than 495 records/users to ensure that the trigger does not put the user too close to the 500 followed-records maximum

The first step uses one SOQL query and the second uses 5, one for each potential follower (5 team member fields).

 

I often update these accounts in batches of over 2,000 at a time (updating account values from an external system using the APEX Data Loader), and I want to be able to do this without hitting governor limits. Any suggestions?

 

What would happen right now if I ran a mass update and it exceeded the 100 SOQL limit? Would it stop the batch update, or just stop running the trigger?

 

Thanks!

 

Here's the trigger:

 

trigger teamMembersFollowRels on Account (after insert, after update) {
    for(Account acc: Trigger.new){
       
       ////Find all of the records followers//// 
       List <EntitySubscription> CurrentSubs = [
           SELECT Id, subscriberid, subscriber.name
           FROM EntitySubscription
           Where parentid =: acc.id
       ];
       
       ////Put the records followers in a map so that they can be checked vs. the potential followers later////
       Map<Id,EntitySubscription> CurrentFollowersMap = new Map<Id,EntitySubscription>();
       for(EntitySubscription Ent: CurrentSubs){
           CurrentFollowersMap.put(Ent.subscriberid, Ent);
       }   
       
       Integer FollowMax = 495;
       
       ////Check each potential followers # of records followed for comparison vs. the max later////
       Integer PMFollowNum = [SELECT COUNT() FROM EntitySubscription WHERE subscriberid = :acc.Portfolio_Manager_Lookup__c];
       Integer TOFollowNum = [SELECT COUNT() FROM EntitySubscription WHERE subscriberid = :acc.Trust_Officer__c];
       Integer WAFollowNum = [SELECT COUNT() FROM EntitySubscription WHERE subscriberid = :acc.Wealth_Advisor__c];
       Integer OTFollowNum = [SELECT COUNT() FROM EntitySubscription WHERE subscriberid = :acc.Other_Team_Member_1__c];
       Integer OTTFollowNum = [SELECT COUNT() FROM EntitySubscription WHERE subscriberid = :acc.Other_Team_Member_2__c];
       
       system.debug('PM ID: ' + acc.Portfolio_Manager_Lookup__c);
       system.debug('PMFollowNum: ' + PMFollowNum);
       
       
        
//**** INSERT SECTION ****//  
        
        if(Trigger.isInsert){
            ////Create EntitySubscription for user in the PM field////
            if(acc.Portfolio_Manager_Lookup__c != Null){
                EntitySubscription followPM = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Portfolio_Manager_Lookup__c
                );
                ////Make sure the user is not already a follower of the record AND is not following more than the max number of records allowed////
                if(!CurrentFollowersMap.containsKey(acc.Portfolio_Manager_Lookup__c) && PMFollowNum<FollowMax){
                    insert followPM;                    
                }
            }
            if(acc.Trust_Officer__c != Null){
                EntitySubscription followTO = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Trust_Officer__c
                );
                if(!CurrentFollowersMap.containsKey(acc.Trust_Officer__c) && TOFollowNum<FollowMax){
                    insert followTO;                    
                }                
            }
            if(acc.Wealth_Advisor__c != Null){
                EntitySubscription followWA = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Wealth_Advisor__c
                );
                if(!CurrentFollowersMap.containsKey(acc.Wealth_Advisor__c) && WAFollowNum<FollowMax){
                    insert followWA;
                }
            }
            if(acc.Other_Team_Member_1__c != Null){
                EntitySubscription followOT = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Other_Team_Member_1__c
                );
                if(!CurrentFollowersMap.containsKey(acc.Other_Team_Member_1__c) && OTFollowNum<FollowMax){
                    insert followOT;
                }
            }
            if(acc.Other_Team_Member_2__c != Null){
                EntitySubscription followOTT = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Other_Team_Member_2__c
                );
                if(!CurrentFollowersMap.containsKey(acc.Other_Team_Member_2__c) && OTTFollowNum<FollowMax){
                    insert followOTT;
                }
            }
            
//**** UPDATE SECTION ****//            
            
        }
        if (Trigger.isUpdate){
            if(acc.Portfolio_Manager_Lookup__c != Null){
                if(acc.Portfolio_Manager_Lookup__c != Trigger.oldMap.get(acc.ID).Portfolio_Manager_Lookup__c){
                    EntitySubscription followPM = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Portfolio_Manager_Lookup__c
                    );
                    if(!CurrentFollowersMap.containsKey(acc.Portfolio_Manager_Lookup__c) && PMFollowNum<FollowMax){
                        insert followPM;                    
                    }
                }
            }
            if(acc.Trust_Officer__c != Null){
                if(acc.Trust_Officer__c != Trigger.oldMap.get(acc.ID).Trust_Officer__c){
                    EntitySubscription followTO = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Trust_Officer__c
                    );
                    if(!CurrentFollowersMap.containsKey(acc.Trust_Officer__c) && TOFollowNum<FollowMax){
                        insert followTO;                    
                    }
                }
            }
            if(acc.Wealth_Advisor__c != Null){
                if(acc.Wealth_Advisor__c != Trigger.oldMap.get(acc.ID).Wealth_Advisor__c){
                    EntitySubscription followWA = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Wealth_Advisor__c
                    );
                    if(!CurrentFollowersMap.containsKey(acc.Wealth_Advisor__c) && WAFollowNum<FollowMax){
                        insert followWA;
                    }
                }
            }
            if(acc.Other_Team_Member_1__c != Null){
                if(acc.Other_Team_Member_1__c != Trigger.oldMap.get(acc.ID).Other_Team_Member_1__c){
                    EntitySubscription followOT = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Other_Team_Member_1__c
                    );
                    if(!CurrentFollowersMap.containsKey(acc.Other_Team_Member_1__c) && OTFollowNum<FollowMax){
                        insert followOT;
                    }
                }
            }
            if(acc.Other_Team_Member_2__c != Null){
                if(acc.Other_Team_Member_2__c != Trigger.oldMap.get(acc.ID).Other_Team_Member_2__c){
                    EntitySubscription followOTT = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Other_Team_Member_2__c
                    );
                    if(!CurrentFollowersMap.containsKey(acc.Other_Team_Member_2__c) && OTTFollowNum<FollowMax){
                        insert followOTT;
                    }
                }
            }
        }
    }
}

 Thanks again!