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
Pankaj_GanwaniPankaj_Ganwani 

Sending an email alert when query rows are more than 50K

Hi Experts,

We have a apex job in which we are fetching some records(SOQL) in execute method based on the record values retured by start method. But, there are more than 50000 records corresponding to one particular value in an object. My requirement is to send an email alert from batch apex if records returned by SOQL are more than 50K. We know that limit exceptions cannot be handled through try/catch blocks in apex.

Is there any workaround to accomplish this? Please find below mentioned code:
 
global class BatchUpdateOptOutPermissions implements Database.Batchable<sObject>
{
   
   /* Start - Variables */
   /* End - Variables */
   
   /* Start - Constructor */
   global BatchUpdateOptOutPermissions()
   {
        //Do Nothing
   }
   /* End - Constructor */

   global Database.QueryLocator start(Database.BatchableContext BC)
   {
      DateTime dtLast24Hours = System.now().addHours(-24);
      return Database.getQueryLocator('SELECT Id, Channel_Value__c FROM Brand_Permission__c WHERE Permission_Value__c = \'OUT\' AND LastModifiedDate >: dtLast24Hours');
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope)
   {
        List<Brand_Permission__c> lstBP = (List<Brand_Permission__c>)scope;// Casting the scope to list of Brand_Permission__c.
        List<Brand_Permission__c> lstBPToUpdate = new List<Brand_Permission__c>();// List to hold the existing channel permission records which needs to updated.
        Set<String> setPermissionValues = new Set<String>();// Set to hold the channel permission values corresponding to those permissions that are passed from the batch.
        
        //Iterating to hold the channel values
        for(Brand_Permission__c objBP : lstBP)
        {
            if(String.isNotEmpty(objBP.Channel_Value__c))
                setPermissionValues.add(objBP.Channel_Value__c);
        }
        
        if(!setPermissionValues.isEmpty())
        {
            //Fetching the existing permissions which are not opted out in last 24 hours.
            for(Brand_Permission__c objBP : [SELECT ID FROM Brand_Permission__c WHERE Permission_Value__c != 'OUT' AND Channel_Value__c IN : setPermissionValues])
            {
                lstBPToUpdate.add(new Brand_Permission__c(Id = objBP.Id, Source_Time_Stamp__c = System.now(), Effective_Date__c = System.now(), Permission_Value__c = 'OUT', Source_Application__c = 'SharedOptOutSource'));
            }
        }
        
       Database.update(lstBPToUpdate,false);
   }

   global void finish(Database.BatchableContext BC)
   {
        //Do Nothing
   }
}

Thanks,
Pankaj
Nachu RY 4Nachu RY 4
put the Try catch method in 36th line. Put "for loop " in the TRY method and put the  "Send email" in the CATCH method.
Pankaj_GanwaniPankaj_Ganwani
Hi Nachu,

Thank you for your reply. But, I tried with the same earlier and it does not seem to be working as we cannot handle limit exceeded exceptions using try/catch block.
Nachu RY 4Nachu RY 4
Please try this

AggregateResult[] groupedResults
  = [SELECT count(id ) FROM  Brand_Permission__c WHEREPermission_Value__c != 'OUT' AND Channel_Value__c IN : setPermissionValues]];

if(groupedResults[0].get('expr0') > 50000){
//send email method

}

else{
//Your code

}
Pankaj_GanwaniPankaj_Ganwani
I also tried with this and it still showing this error. 50K limit still applies on AggregateResult class.
Nachu RY 4Nachu RY 4
Pleasse try below:

integer i = 0
List <Brand_Permission__c> bb = [select id from Brand_Permission__c limit 25000 offset:i];
i = 25000;
bb = [select id from Brand_Permission__c limit 25000 offset:i];
i=50000
bb = [select id from Brand_Permission__c limit 25000 offset:i];

if(bb.size()  > 50000){
//send email method

}

else{
//Your code

for(Brand_Permission__c objBP : [SELECT ID FROM Brand_Permission__c WHEREPermission_Value__c != 'OUT' AND Channel_Value__c IN : setPermissionValues])
     {               lstBPToUpdate.add(new Brand_Permission__c(Id = objBP.Id, Source_Time_Stamp__c= System.now(), Effective_Date__c = System.now(), Permission_Value__c = 'OUT',Source_Application__c = 'SharedOptOutSource'));
           }

}
Pankaj_GanwaniPankaj_Ganwani
Hi Nachu,

Thank you for all your help. Unfortunately, the above solution will not work too since we can set the value for offset upto 2000.