+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Batch class for sending an email notification to public group user

Dear Developers,

I would like to send notification of public group users by using batch class(weekly based). can anyone please advise me how can i change my code for accurate requirement.

Batch Class 
global class PSO_etmfErrorLogBatch implements database.batchable<SObject>{

global database.querylocator start(database.batchableContext bc){
    system.debug('Start Method Running');
    return Database.getQueryLocator('Select Id,name from eTMF_Files_Error_Log__c');
}

global void execute(database.batchableContext bc, List<eTMF_Files_Error_Log__c> errlogList){
    system.debug('Execute Method Running');
    List<String> toAddresses = new List<String>();
    List<User> userList = [select id, name, email, isactive, profile.name, userrole.name, usertype 
                           from user 
                           where id in (select userorgroupid from groupmember where group.name = 'Document Tracking Admin')];
    for(User u : userList){           
        toAddresses.add(u.email);
    }  
    List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
    for (eTMF_Files_Error_Log__c irow_erLog : errlogList) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(toAddresses});
        email.setSubject('Last Week ETMF Error Log'); 
        email.setPlainTextBody('Dear user, Please Check Last Week ETMF Error Log');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}

global void finish(database.batchableContext bc){}

}

Hearty Thanks In Advance !!

Regards,
Soundar P
Best Answer chosen by Soundar Raj Ponpandi
Raj VakatiRaj Vakati
Try this only  .. i prefer not to do in the finish because of the governed limits 
 
global class PSO_etmfErrorLogBatch implements database.batchable<SObject>, Database.Stateful{

global database.querylocator start(database.batchableContext bc){
    
    return Database.getQueryLocator('Select Id,name from eTMF_Files_Error_Log__c');
}

global void execute(database.batchableContext bc, List<Sobject> obj){
   
   
   List<String> toAddresses = new List<String>();
		List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
		
		List<User> userList = [SELECT Id, Name, Email, isActive, Profile.Name, userrole.name, usertype 
							   FROM User 
							   WHERE Id IN (SELECT userorgroupid FROM groupmember WHERE group.name = 'Document Tracking Admin')];
		for(User u : userList){           
			toAddresses.add(u.email);
		}  
		
		List<eTMF_Files_Error_Log__c> lstErrorlog = (List<eTMF_Files_Error_Log__c> )obj ; 
		
		for (eTMF_Files_Error_Log__c irow_erLog : lstErrorlog) { //Note Here you are
			Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
			email.setToAddresses(toAddresses});
			email.setSubject('Last Week ETMF Error Log'); 
			email.setPlainTextBody('Dear user, Please Check Last Week ETMF Error Log');
			emails.add(email);
		}
		Messaging.sendEmail(emails);
		
		
    
}

global void finish(database.batchableContext bc){}
	
}

 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Sundar P,

I have updated you code and try this. Let me know if it help you.
global class PSO_etmfErrorLogBatch implements database.batchable<SObject>, Database.Stateful{
	public List<eTMF_Files_Error_Log__c> lstErrorlog;

global database.querylocator start(database.batchableContext bc){
    lstErrorlog = new List<eTMF_Files_Error_Log__c>();
	system.debug('Start Method Running');
    return Database.getQueryLocator('Select Id,name from eTMF_Files_Error_Log__c');
}

global void execute(database.batchableContext bc, List<eTMF_Files_Error_Log__c> errlogList){
    system.debug('Execute Method Running');
	
	if(!errlogList.isEmpty()) {
		lstErrorlog.addAll(errlogList);
	}
    
}

global void finish(database.batchableContext bc){}
	if(!lstErrorlog.isEmpty()) {
		List<String> toAddresses = new List<String>();
		List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
		
		List<User> userList = [SELECT Id, Name, Email, isActive, Profile.Name, userrole.name, usertype 
							   FROM User 
							   WHERE Id IN (SELECT userorgroupid FROM groupmember WHERE group.name = 'Document Tracking Admin')];
		for(User u : userList){           
			toAddresses.add(u.email);
		}  
		
		for (eTMF_Files_Error_Log__c irow_erLog : lstErrorlog) { //Note Here you are
			Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
			email.setToAddresses(toAddresses});
			email.setSubject('Last Week ETMF Error Log'); 
			email.setPlainTextBody('Dear user, Please Check Last Week ETMF Error Log');
			emails.add(email);
		}
		Messaging.sendEmail(emails);
	}
}
Thanks
Niraj
 
Raj VakatiRaj Vakati
Try this only  .. i prefer not to do in the finish because of the governed limits 
 
global class PSO_etmfErrorLogBatch implements database.batchable<SObject>, Database.Stateful{

global database.querylocator start(database.batchableContext bc){
    
    return Database.getQueryLocator('Select Id,name from eTMF_Files_Error_Log__c');
}

global void execute(database.batchableContext bc, List<Sobject> obj){
   
   
   List<String> toAddresses = new List<String>();
		List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
		
		List<User> userList = [SELECT Id, Name, Email, isActive, Profile.Name, userrole.name, usertype 
							   FROM User 
							   WHERE Id IN (SELECT userorgroupid FROM groupmember WHERE group.name = 'Document Tracking Admin')];
		for(User u : userList){           
			toAddresses.add(u.email);
		}  
		
		List<eTMF_Files_Error_Log__c> lstErrorlog = (List<eTMF_Files_Error_Log__c> )obj ; 
		
		for (eTMF_Files_Error_Log__c irow_erLog : lstErrorlog) { //Note Here you are
			Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
			email.setToAddresses(toAddresses});
			email.setSubject('Last Week ETMF Error Log'); 
			email.setPlainTextBody('Dear user, Please Check Last Week ETMF Error Log');
			emails.add(email);
		}
		Messaging.sendEmail(emails);
		
		
    
}

global void finish(database.batchableContext bc){}
	
}

 
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi  Niraj Kr Singh & Raj Vakati,

Thanks for your valuable responses.

Regards,
Soundar Rajan P.