+ Start a Discussion
Kiran Jain 15Kiran Jain 15 

Scheduler Class is executing automatically

Hy Experts,
I have a batch class that send an  email on updating contact record,that is calling  from scheduler and  scheduler is calling from after  update on Contact.
My Problam is that I'm not updating any contact still my Batch is running  and sending email.
Below is my resources.
//Trigger
//Trigger
//Trigger
trigger maintrigger on Contact (after update) {
    //public static String CRON_EXP = '2 0 0 2 6 ? 2022';
    //DateTime dtTemp = Datetime.now().addMinutes(1);
    DateTime dtTemp = Datetime.now().addSeconds(30);
String hour = String.valueOf(dtTemp.hour());
String min = String.valueOf(dtTemp.minute()); 
String ss = String.valueOf(dtTemp.second());
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
    Map<id,contact> IdContactMap=new Map<Id,contact>();
    for(Contact con:trigger.new){
        if(con.email!=Null){
            IdContactMap.put(con.id,con);
        }
    }
    if(IdContactMap.size() > 0){
     //   String jobId = System.schedule('call scheduable apex class', nextFireTime, new MailToNewContactSch(IdContactMap));
    }

}
I have commented that part  in trigger from where scheduler is calling but still is running and sending email.
//Scheduler
global class MailToNewContactSch implements Schedulable {
    
     Map<id,contact> IdContactMap=new Map<Id,contact>();
    public MailToNewContactSch( Map<id,contact> contactIdMap) {
        IdContactMap = contactIdMap;
    }
/*String sch2 = '5 * * * ?';
scheduledQuoteReminderBatchable sqrb2 = new scheduledQuoteReminderBatchable();
system.schedule('Every Hour plus 5 min', sch2, sqrb2);*/

    global void execute(SchedulableContext sc) { 
        system.debug('current time : ' + system.now());
        system.debug('IdContactMap : ' + IdContactMap);
       // MailToNewContact mailNewConBatch =  new MailToNewContact(IdContactMap);
       // Database.executeBatch(mailNewConBatch, 200);
    }
}
//Batch
global class MailToNewContact implements Database.Batchable<sObject>
{
    Map<id,contact> IdContactMapBatch=new Map<id,contact>();
    global MailToNewContact(Map<id,contact> IdContactMap){
        IdContactMapBatch=IdContactMap;
    }
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([SELECT id,firstname,email from contact where id in:IdContactMapBatch.keySet()]);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope) {     
        for(Contact con : scope)
        {    
            system.debug('con.email : ' + con.email);
           Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
               email.setToAddresses(new string[] {con.email});
               email.setSubject('Welcome Mail from this org');
               email.setPlainTextBody('Welcome Your Contact Is Created Successfully In Salesforce');
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }
    global void finish(Database.BatchableContext BC)
    {
    }
}

Please  help me  to resolving it.
Thanks in advance



 
AbhinavAbhinav (Salesforce Developers) 
Hi Kiran,

Have you double checked the logs when Batch is getting executed?

Thanks