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
nilesh walkenilesh walke 

Write a batch class to send an email to Opportunity owner. On 1st of each month send an email to Opportunity owner if close date is in this month. please give explnation after code thanks

PriyaPriya (Salesforce Developers) 

Hi Nilesh,

This is just a pseudocode do make the required correction as you may seem fit 

global class sendEmail implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Owner.Email from Opportunity where stage = 'Closed' and CALENDAR_MONTH(ClosedDate) = CALENDAR_MONTH(CreatedDate)';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {       
        EmailTemplate emailTemplate = [select Id, Body from EmailTemplate where DeveloperName = 'testTemplate'];
        
        for(Opportunity op : (List<Opportunity>)scope) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {op.Owner.Email});
            email.setSaveAsActivity(false);
            email.setTargetObjectId(op.OwnerId);
            email.setTemplateId(emailTemplate.Id);
            email.setWhatId(op.Id);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

If above information helps then please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

Suraj Tripathi 47Suraj Tripathi 47

Hi Nilesh,

Please find the solution.

Batch Class:

public class BatchOnOpportunity implements DataBase.Batchable<sObject>{
    
    public List<Opportunity> oppList{set;get;}
    public List<String> emailList{set;get;}
    public BatchOnOpportunity(){
        oppList=[select id,OwnerId,Owner.Email,CloseDate from Opportunity where CloseDate=THIS_MONTH];
    }
    
    public List<Opportunity> start(DataBase.BatchableContext BC){
        return oppList;
    }
    public void execute(Database.BatchableContext BC,List<Opportunity> opportunityList){
        emailList=new List<String>();
        for(Opportunity op:opportunityList){
            emailList.add(op.Owner.Email);
        }
        system.debug('emailList::::'+emailList);
        sendmail();
    }
    public void sendmail()
    { 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       
             email.setSubject(' Close Date Reminder');
        email.setPlainTextBody('Your close date is belongs to this Month');
        email.setToAddresses(emailList);
        Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
    }
    public void finish(Database.BatchableContext BC){
         system.debug('emailList finish::::'+emailList);
    }
}
Schedule Class:
global class BatchScheduleOnOportunity implements Schedulable  {
    global void execute(SchedulableContext sc) 
    {
        BatchOnOpportunity bObj = new BatchOnOpportunity(); 
        database.executebatch(bObj);
    }
}


You can take reference for scheduling class from the below link:

http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Please let me know  you are abale to schedule or not.

Please mark it as Best Answer

Thank You