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
mohan s 37mohan s 37 

How to send a mail after completing the batch

This is my code
=============
global class BatchApex implements Database.Batchable<sobject>{
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('select id from Opportunity');
        
    }
    global void execute(Database.BatchableContext bc,List<Opportunity>scope){
        for(Opportunity o:scope){
            o.Name='sandhya';
        }
    update scope;
}
    global void finish(Database.BatchableContext bc){
            
   Messaging.SingleEmailMessage[] mail=new Messaging.SingleEmailMessage();
        String[] toAddresses=new String[] {'forcesfdcloud@gmail.com'};
            mail.setToAddresses(toAddresses);
        mail.setSubject('sending mail ');
        mail.setPlainTextBody('process completed success fully');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
      
    }
    }
I want to update my opportunities. After updating the opportunities i want to send a mail to the above mentioned address, but it shows error like" Illegal assignment from Messaging.SingleEmailMessage to List<Messaging.SingleEmailMessage>" can any one tell me how to solve this issue.
Best Answer chosen by mohan s 37
Amit Chaudhary 8Amit Chaudhary 8

Please check below post for more information
1) http://amitsalesforce.blogspot.in/search/label/Email

Please update your code like below
global class BatchApex implements Database.Batchable<sobject>
{
    global Database.QueryLocator start(Database.BatchableContext bc)
	{
        return Database.getQueryLocator('select id from Opportunity');
        
    }
    global void execute(Database.BatchableContext bc,List<Opportunity>scope)
	{
			for(Opportunity o:scope){
				o.Name='sandhya';
			}
		update scope;
	}
    global void finish(Database.BatchableContext bc)
	{
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
        String[] toAddresses=new String[] {'forcesfdcloud@gmail.com'};
		mail.setToAddresses(toAddresses);
		mail.setSubject('sending mail ');
        mail.setPlainTextBody('process completed success fully');
		
		Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {mail};
		Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
      
    }
}
Let us know if this will help you