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
Rija SanaRija Sana 

help with turning trigger into batch apex code

So the trigger i have is working fine in production except that when our pipeline is updating sfdc during off hours I get email notification 'Apex script unhandled trigger exception by user/organization: BlockUsers: System.LimitException: Too many SOQL queries: 101'

I am new to apex even newer to batch apex. Can someone help me with turning this code into batch apex trigger 
 
trigger BlockUsers on Account  (before insert, before update) {

Account accs = Trigger.new[0];
string accExec = accs.Assigned_AE__c;
integer i=[select count() from account where Assigned_AE__c= :accExec AND Assigned_AE__c!=null];
for(Account a: Trigger.new){
 If (trigger.isInsert || (trigger.isUpdate && trigger.newMap.get(a.Id).Assigned_AE__c != trigger.oldMap.get(a.Id).Assigned_AE__c))
     {          
                if(i >=100)
                a.addError('The AE you are trying to add already has 100 accounts in their name');
}
}
}

trigger gets activated whenever an update happens on assigned_ae__c field. This code is reteiving all accounts where that particualar AE is listed and then counts them and as long as the number is less than 100 allows the update to happen.
 
Best Answer chosen by Rija Sana
Maharajan CMaharajan C
Hi Rija,

Try with below trigger which is using the Aggregate Function with Bulkification:


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

    set<string> assignedAEset = new set<String>();
    Map<String,Integer> assignedAEcountMap = new Map<String,Integer>();
    
    for(Account acc:Trigger.New)
    {
        if(acc.Assigned_AE__c != null)
        {
            assignedAEset.add(acc.Assigned_AE__c);
        }
    }
    
    if(assignedAEset.size() > 0)
    {
        for(AggregateResult ar: [SELECT count(id) totalcount,Assigned_AE__c from Account 
                                 where Assigned_AE__c IN:assignedAEset Group By Assigned_AE__c])
        {
            assignedAEcountMap.put(String.ValueOf(ar.get('Assigned_AE__c')),Integer.ValueOf(ar.get('totalcount')));  
        }
    }
    
        for(Account a:Trigger.New)
        {
            If (trigger.isInsert || (trigger.isUpdate && trigger.newMap.get(a.Id).Assigned_AE__c != trigger.oldMap.get(a.Id).Assigned_AE__c))
            {
                if(assignedAEcountMap.containsKey(a.Assigned_AE__c))
                {
                    Integer i = assignedAEcountMap.get(a.Assigned_AE__c);
                    if(i > 100)
                    {
                        a.addError('The AE you are trying to add already has 100 accounts in their name');
                    }
                        
                }
            }
        }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Shravan Kumar 71Shravan Kumar 71
Hi Rija,

Here is the link for similar discussion which will help you : https://developer.salesforce.com/forums/?id=9060G000000IAFPQA4

Let us know in case if you need any further help.

Thanks,
Shravan
Maharajan CMaharajan C
Hi Rija,

Try with below trigger which is using the Aggregate Function with Bulkification:


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

    set<string> assignedAEset = new set<String>();
    Map<String,Integer> assignedAEcountMap = new Map<String,Integer>();
    
    for(Account acc:Trigger.New)
    {
        if(acc.Assigned_AE__c != null)
        {
            assignedAEset.add(acc.Assigned_AE__c);
        }
    }
    
    if(assignedAEset.size() > 0)
    {
        for(AggregateResult ar: [SELECT count(id) totalcount,Assigned_AE__c from Account 
                                 where Assigned_AE__c IN:assignedAEset Group By Assigned_AE__c])
        {
            assignedAEcountMap.put(String.ValueOf(ar.get('Assigned_AE__c')),Integer.ValueOf(ar.get('totalcount')));  
        }
    }
    
        for(Account a:Trigger.New)
        {
            If (trigger.isInsert || (trigger.isUpdate && trigger.newMap.get(a.Id).Assigned_AE__c != trigger.oldMap.get(a.Id).Assigned_AE__c))
            {
                if(assignedAEcountMap.containsKey(a.Assigned_AE__c))
                {
                    Integer i = assignedAEcountMap.get(a.Assigned_AE__c);
                    if(i > 100)
                    {
                        a.addError('The AE you are trying to add already has 100 accounts in their name');
                    }
                        
                }
            }
        }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Rija SanaRija Sana
Thanks Maharanjan!. I have tested in sandbox and is working fine. I have yet to deploy this in production which is where I was seeing the error before.