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
Rakesh SamalRakesh Samal 

I want to send email to CEO when how many trainers is found unverified in batch class finish method

Custom object: Trainer_Master__c
picklist Field: Verification_status__c(details needed, verified, unverified)

how many trainers are found unverified? Then Send email to CEO in batch apex finish method. This all work should be on finish method batch class.

Please help me on this
AnkaiahAnkaiah (Salesforce Developers) 
Hi Rakesh,

Do you want to send email on daily basis?

Thanks!!
Rakesh SamalRakesh Samal
Hi Ankaiah,
No. on Friday 6 PM only which I have done using CRON Expression. 

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Rakesh,

try with below code. 
public class UnvarifiedTrainersBatch implements database.Batchable<Sobject> {
    
    public Database.QueryLocator start(Database.BatchableContext bc){
   String vfstatus='unvarified';
   String str ='select id from Trainer_Master__c where Verification_status__c=:vfstatus';
return Database.getQueryLocator (str);
}

    Public void execute(Database.BatchableContext bc,List<Trainer_Master__c> scope){
        set<id> countofids = new set<id>();
        for(Trainer_Master__c con:scope){
        countofids.add(con.id);
        }
        
if(countofids.size()>0){
                        
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'ceo@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('count of unvarified trainers');
mail.setPlainTextBody('Total unvarified trainers count is ' +'  '+ countofids.size());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            
        }       


    }       

 Public void finish(Database.BatchableContext bc){        
 } 

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Rakesh SamalRakesh Samal
Hi Ankaiah,
Can I write this code on finish method? Because we have to write code in finish method
Rakesh SamalRakesh Samal
Please check this. If i am using string vfstatus = 'Non Verified'; then my condition not working. only this working. How to write this part in finish method??



global class TrainerMasterClass implements Database.Batchable<sObject>,Database.Stateful {
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
       // string vfstatus = 'Non Verified';
        string query = 'SELECT LinkedIn_Profile__c,Background_Check_Done__c,Verification_Status__c FROM Trainer_Master__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, list<Trainer_Master__c> scope)
    {
        for(Trainer_Master__c varTrainerMaster:scope)
        {
            if(varTrainerMaster.LinkedIn_Profile__c==null)
            {
                varTrainerMaster.Verification_Status__c = 'Details Needed';
            }
            else if(varTrainerMaster.LinkedIn_Profile__c!=null && varTrainerMaster.Background_Check_Done__c=='No')
            {
                varTrainerMaster.Verification_Status__c = 'Non Verified';
            }
            else if(varTrainerMaster.LinkedIn_Profile__c!=null && varTrainerMaster.Background_Check_Done__c=='Yes')
            {
                varTrainerMaster.Verification_Status__c = 'Verified';
            }
        }
        update scope;
    }
    
    global void finish(Database.BatchableContext BC)
    {
       // list<Trainer_Master__c> scope = New list<Trainer_Master__c>();
       // set<id> countofids = new set<id>();
       // for(Trainer_Master__c con:scope){
     //   countofids.add(con.id);
       // }
        
         //   if(countofids.size()>0){
                                    
           // Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         //   String[] toAddresses = new String[] {'rakeshsamal0323@gmail.com'};
           // mail.setToAddresses(toAddresses);
       //     mail.setSubject('count of unvarified trainers');
         //   mail.setPlainTextBody('Total unvarified trainers count is ' +'  '+ countofids.size());
           // Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                        
             //       }
    }
}
Rakesh SamalRakesh Samal
Hello Ankaiah,

Thanks for ur answer. I modified my code in finish method and it is working correctly.