+ Start a Discussion
Kiran Jain 15Kiran Jain 15 

Batch Class is executing automatically

Hy Experts,
I have a batch class that send an  email on updated 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 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));
    }

}
 
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) { 
       MailToNewContact mailNewConBatch =  new MailToNewContact(IdContactMap);
       Database.executeBatch(mailNewConBatch, 200);
    }
}
 
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 achieve this requirment.
Thanks in advance
CharuDuttCharuDutt
Hii Kiran
Try Below 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 && con.email != Trigger.oldMap.get(con.Id).email){
            IdContactMap.put(con.id,con);
        }
    }
    if(IdContactMap.size() > 0){
     String jobId = System.schedule('call scheduable apex class', nextFireTime, new MailToNewContactSch(IdContactMap));
    }

}
Please Mark It As Best Answer If It Helps
Thank You!