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
Nitin SharmaNitin Sharma 

Created a schedulable class for batch job but its not working.

Batch class
global class BirthdayNotificationBatch Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        Date tody = system.today();
        String SOQL = 'SELECT Id, Name, Email__c FROM Contact WHERE DAY_IN_MONTH(Birthdate) = ' + 
                      tody.day() +  ' AND CALENDAR_MONTH(Birthdate) = ' + tody.month();
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<Contact> listContacts) {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Contact c : listContacts) {
            List<String> toAddresses = new List<String>{c.Email};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Happy Birthday');
            String messageBody = '<html><body>Hi ' + c.Name + ',<br/>Happy Birthday.<br/>Many More Happy '+
                                 'Returns of the day.<br/><br/><b>Regards,</b><br/>Admin</body></html>';
            mail.setHtmlBody(messageBody); 
            mailList.add(mail);          
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}
Schedule class
global class ScheduleBatch Implements Schedulable {
    global void execute(SchedulableContext sc){
        BirthdayNotificationBatch sb = new BirthdayNotificationBatch();
        Database.executeBatch(sb);
    }
    ScheduleBatch my = new ScheduleBatch();
    String str = '0 30 11 * 5 ?';
    System.schedule('sb', str, my);
    
}
But its not working.
Anyone can solve this issue.

Thanks
Nitin Sharma

 
Best Answer chosen by Nitin Sharma
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for batch job related requirement
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

How to Schedule scheduler class
There are two option we have schedule the scheduler classes.
1) By System Scheduler.
2) By Developer console

System Scheduler.

Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time like below screen shot.

By Developer console 
Execute below code from developer console :-
ScheduleBatch  m = new ScheduleBatch ();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);

1) http://www.simplysfdc.com/2015/02/salesforce-send-birthday-email.html
Sample code for you
global class BirthdayCronJob implements Schedulable
{    
	global void execute(SchedulableContext SC) 
	{  
		sendmail(); // our main function that does the email sending  
	}

	public List<Id> getBirthdayEmailAddresses(Integer Month, Integer Day)   
	{   
		List<Id> mailToIds = new List<Id>();  
		   
		Contact[] c = [SELECT Id, email, Birthdate, Send_Birthday_Email__c, HasOptedOutOfEmail  
				FROM Contact   
				WHERE email <> '' AND   
				DAY_IN_MONTH(Birthdate) = : Day   
				AND CALENDAR_MONTH(Birthdate) = : Month    
				];  

		for(Contact recipient : c) 
		{  
			if (recipient.Send_Birthday_Email__c == true && recipient.HasOptedOutOfEmail == false)  
			{  
			  mailToIds.add(recipient.Id);  // add to email contact array  
			} else {  
			  System.Debug('\n*******NO Recipient');  
			}  
		}
		return mailToIds;  
	}
    public void sendMail()   
    {  
        String debugAddress = 'eyewell@salesforce.com';  // add your email address
        String BirthdayEmailTemplateName = 'Happy_Birthday';  // add valid email template name
        String AnniversaryEmailTemplateName = 'Celebrating_your_Anniversary';        
        String debugMessage;  
        String[] toAddresses;  
        Integer DayOfEvent  = date.today().day();  
        Integer MonthOfEvent = date.today().month();  
        List<Id> BirthdayIdsList = getBirthdayEmailAddresses(MonthOfEvent,DayOfEvent);  
		EmailTemplate birthdayTemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :BirthdayEmailTemplateName];  
        if(birthdayTemplate != null && BirthdayIdsList.isEmpty() == false)  
        {  
          Messaging.MassEmailMessage birthdayMail = new Messaging.MassEmailMessage();  
          birthdayMail.setTargetObjectIds(BirthdayIdsList);  
          birthdayMail.setTemplateId(birthdayTemplate.Id);  
          birthdayMail.setUseSignature(false);  
          birthdayMail.setSaveAsActivity(true);  
    
			try {  
				Messaging.sendEmail(new Messaging.MassEmailMessage[] { birthdayMail });  
			}catch(Exception e)  
			{  
			System.Debug(e);  
			}  
          
        }  
        else  
		{  
          System.Debug('BirthdayCronJob:sendMail(): Either an email template could not be found, or no Contact has a birthday today');  
        }
    }
}



Please check below post for "Creating an Automatic Birthday Email"
1) https://help.salesforce.com/HTViewSolution?id=000181218&language=en_US


....................................

In your case please update your Batch job like below (i found you was using the email__c field not email field)
global class BirthdayNotificationBatch Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        Date tody = system.today();
        String SOQL = 'SELECT Id, Name, Email FROM Contact WHERE DAY_IN_MONTH(Birthdate) = ' + 
                      tody.day() +  ' AND CALENDAR_MONTH(Birthdate) = ' + tody.month();
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<Contact> listContact) 
	{
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Contact m : listContact) 
		{
            List<String> toAddresses = new List<String>{m.Email};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Happy Birthday');
            String messageBody = '<html><body>Hi ' + m.Name + ',<br/>Happy Birthday.<br/>Many More Happy '+
                                 'Returns of the day.<br/><br/><b>Regards,</b><br/>Admin</body></html>';
            mail.setHtmlBody(messageBody); 
            mailList.add(mail);          
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}

Scheduler class like below
global class ScheduleBatch Implements Schedulable 
{
    global void execute(SchedulableContext sc){
        BirthdayNotificationBatch sb = new BirthdayNotificationBatch();
        Database.executeBatch(sb);
    }   
}
You can excute your batch job from developer console also with below code.
        BirthdayNotificationBatch sb = new BirthdayNotificationBatch();
        Database.executeBatch(sb);


Let us know if this will help you

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for batch job related requirement
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

How to Schedule scheduler class
There are two option we have schedule the scheduler classes.
1) By System Scheduler.
2) By Developer console

System Scheduler.

Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time like below screen shot.

By Developer console 
Execute below code from developer console :-
ScheduleBatch  m = new ScheduleBatch ();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);

1) http://www.simplysfdc.com/2015/02/salesforce-send-birthday-email.html
Sample code for you
global class BirthdayCronJob implements Schedulable
{    
	global void execute(SchedulableContext SC) 
	{  
		sendmail(); // our main function that does the email sending  
	}

	public List<Id> getBirthdayEmailAddresses(Integer Month, Integer Day)   
	{   
		List<Id> mailToIds = new List<Id>();  
		   
		Contact[] c = [SELECT Id, email, Birthdate, Send_Birthday_Email__c, HasOptedOutOfEmail  
				FROM Contact   
				WHERE email <> '' AND   
				DAY_IN_MONTH(Birthdate) = : Day   
				AND CALENDAR_MONTH(Birthdate) = : Month    
				];  

		for(Contact recipient : c) 
		{  
			if (recipient.Send_Birthday_Email__c == true && recipient.HasOptedOutOfEmail == false)  
			{  
			  mailToIds.add(recipient.Id);  // add to email contact array  
			} else {  
			  System.Debug('\n*******NO Recipient');  
			}  
		}
		return mailToIds;  
	}
    public void sendMail()   
    {  
        String debugAddress = 'eyewell@salesforce.com';  // add your email address
        String BirthdayEmailTemplateName = 'Happy_Birthday';  // add valid email template name
        String AnniversaryEmailTemplateName = 'Celebrating_your_Anniversary';        
        String debugMessage;  
        String[] toAddresses;  
        Integer DayOfEvent  = date.today().day();  
        Integer MonthOfEvent = date.today().month();  
        List<Id> BirthdayIdsList = getBirthdayEmailAddresses(MonthOfEvent,DayOfEvent);  
		EmailTemplate birthdayTemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :BirthdayEmailTemplateName];  
        if(birthdayTemplate != null && BirthdayIdsList.isEmpty() == false)  
        {  
          Messaging.MassEmailMessage birthdayMail = new Messaging.MassEmailMessage();  
          birthdayMail.setTargetObjectIds(BirthdayIdsList);  
          birthdayMail.setTemplateId(birthdayTemplate.Id);  
          birthdayMail.setUseSignature(false);  
          birthdayMail.setSaveAsActivity(true);  
    
			try {  
				Messaging.sendEmail(new Messaging.MassEmailMessage[] { birthdayMail });  
			}catch(Exception e)  
			{  
			System.Debug(e);  
			}  
          
        }  
        else  
		{  
          System.Debug('BirthdayCronJob:sendMail(): Either an email template could not be found, or no Contact has a birthday today');  
        }
    }
}



Please check below post for "Creating an Automatic Birthday Email"
1) https://help.salesforce.com/HTViewSolution?id=000181218&language=en_US


....................................

In your case please update your Batch job like below (i found you was using the email__c field not email field)
global class BirthdayNotificationBatch Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        Date tody = system.today();
        String SOQL = 'SELECT Id, Name, Email FROM Contact WHERE DAY_IN_MONTH(Birthdate) = ' + 
                      tody.day() +  ' AND CALENDAR_MONTH(Birthdate) = ' + tody.month();
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<Contact> listContact) 
	{
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Contact m : listContact) 
		{
            List<String> toAddresses = new List<String>{m.Email};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Happy Birthday');
            String messageBody = '<html><body>Hi ' + m.Name + ',<br/>Happy Birthday.<br/>Many More Happy '+
                                 'Returns of the day.<br/><br/><b>Regards,</b><br/>Admin</body></html>';
            mail.setHtmlBody(messageBody); 
            mailList.add(mail);          
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}

Scheduler class like below
global class ScheduleBatch Implements Schedulable 
{
    global void execute(SchedulableContext sc){
        BirthdayNotificationBatch sb = new BirthdayNotificationBatch();
        Database.executeBatch(sb);
    }   
}
You can excute your batch job from developer console also with below code.
        BirthdayNotificationBatch sb = new BirthdayNotificationBatch();
        Database.executeBatch(sb);


Let us know if this will help you

Thanks
Amit Chaudhary


 
This was selected as the best answer
Nitin SharmaNitin Sharma
Thanks Amit,
Its working I used wrong field name.