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
DmonikaDmonika 

Problem in Calling Batch Class from Scheduler Apex

Hi,

Can any one please help me out . No error message but not working.

global class sendEmails implements Database.Batchable<sObject> 
{
         List<Messaging.SingleEmailMessage> lMail = new List<Messaging.SingleEmailMessage>();
         
    global Database.QueryLocator start(Database.BatchableContext BC) 
        {
            String query = 'SELECT Id,Name,Pm_email__c FROM Employee__c';
            return Database.getQueryLocator(query);
        }
    global void execute(Database.BatchableContext BC, List<Employee__c> scope) 
        {
            for(Employee__c emp : scope)
            {
                String emails = emp.Pm_email__c; //takes whole emailID in string
                List<String> email = emails.split(',');//divides the string with ',' and stores in a list.
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(email);  
                mail.setSubject('Welcome to Sweet 16 Siebel Batch');
                String messageBody = '<html><body>Hi ' + ',Welcome </body></html>';
                mail.setHtmlBody(messageBody);   
                mail.setSubject('Quality Assurance');
                system.debug(mail);
                
                lMail.add(mail);
            }
                Messaging.sendEmail(lMail);
        }
    global void finish(Database.BatchableContext BC)
        {
            
        }
  
}

=============================

global class ScheduleEmail implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        ID BatchId = Database.executeBatch(new sendEmails(),200);
    }
    public void SchedulerMethod()
    {
        String timer ='0 2 0 * * ?';
        system.Schedule('sendEmailsTest',timer,new ScheduleEmail());
        
    }
}

In anonymous Block i am executing in this way 

ScheduleEmail se = new ScheduleEmail();
String tim ='0 02 9 10 10 ?';
system.debug(tim);
system.schedule('ExecuteScheduler',tim,new ScheduleEmail());


Thanks in advance.
Alexander TsitsuraAlexander Tsitsura
Hi,

All look good for me, try run you job after 1 minute, for it use code below
final DateTime d = DateTime.now().addMinutes(1);
String cronString = d.format('ss mm HH dd MM ? yyyy');
System.schedule('ExecuteScheduler', cronString, new ScheduleEmail());

After that go to Setup -> Monitoring -> Apex Job, you need find job with type "Schedule" and  class "ScheduleEmail" with status queue. After 1 minute, in this list added new job with type "batch" and class "sendEmails". You can see how records chunks retrieved and how mane error occurs.

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
Shashi PatowaryShashi Patowary
Hi Monica,

How frequently do you want to run your job.Cron expression has the following syntax -
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

E.g.  0 0 10 * * ?  (Batch class runs every day at 10 AM)
        0 15 * * * ?   (Batch class runs every 15 mins )

Now looking at your cron expression -    '0 02 9 10 10 ?'   ,value entered for minutes seems to be wrong (02). Values should be 0 to 59.
If you had tried to execute the batch at 9.02 AM on 10th October every year, the correct cron expression should be like this -

 '0 2 9 10 10 ?';

Please let us know if it works for you.

Thanks,
Shashi

 
DmonikaDmonika
Hi All,

Thank you for ur support, I got a clear explaination how to use job.Cron.

Its working fine now.

Thanks

Monika.
Shashi PatowaryShashi Patowary
Thanks Monica,

Based on the above answers, please feel free to mark one answer as 'Solved'.

Regards,
Shashi