+ Start a Discussion
Priya AakankshaPriya Aakanksha 

Batch class to be written on Lead object that runs every hour.

Reminder Email:
Batch class to be written on Lead object that runs every hour.
Criteria:
If the is no activity created for a lead (with Source = Web or Phone) for 2 hours after the lead creation time, a reminder email is to go to the lead owner.
Email Content:
It has been two hours since this lead is created. Please contact the customer.
Detail Link

A task also has to be created.
Subject: Reminder
Comments: It has been two hours since this lead is created and no activity is created. Please contact the customer.
Status : Open
Priority: Medium
Due date : Today
Assigned To: Lead Owner
====================================
i wrote batch class but its showing error 

global class Lead_Escalation implements database.Batchable<sobject>,schedulable{
    global database.QueryLocator start(database.BatchableContext bc){
       
         integer day=system.today().day();
         string  query;
       // string a ='Website' ;
        //string b ='Phone' ;
         if(!test.isRunningTest()){
             query='select id,Name,Owner.ID,Assigned To,activityid,owner.firstname,owner.lastname,Owner.Email,LeadSource from lead where activityid=null AND (LeadSource=:Website OR LeadSource=:Phone) order by id';
            }else{
                query='select id,Name,Owner.ID,owner.firstname,owner.lastname,Owner.Email,LeadSource from lead limit 1';
            }
            
            return database.getQueryLocator(query);
    }
    
    global void execute(database.BatchableContext bc,list<lead>ldlist){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
     
        list<task> tklist=new list<task>();
        for(lead l:ldlist){
        
                string body= 'Dear '+' '+'l.owner.name'+'l.name'+'\n'+
                    'It has been two hours since this lead is created. Please contact the customer';
            {  
                   Messaging.SingleEmailMessage message= new Messaging.SingleEmailMessage();
                message.setSubject('Reminder :Enquiry for '+'l.name' + ' to be acted on.');
          //      mail.settoaddresses(toadd);
                message.setBccAddresses(new string[]{'priyaaakanksha28@gmail.com'});
                message.setsenderdisplayname('NN');
                message.setSaveAsActivity(false);
                message.setHtmlBody(body);
                mails.add(message);
                
             /*   
               task tasks=new task();       
                tasks.WhoId=l.id;
                tasks.OwnerId=l.ownerid;
                
               tasks.Subject='Reminder';
               tasks.Description='Dear '+' '+'l.owner.name'+'l.name'+'\n'+
                    'It has been two hours since this lead is created. Please contact the customer'.
               tasks.TaskInfo__c='alert';
               tasks.Priority='Medium';
               tasks.Status='Open';
         //      tasks.Assigned To='Owner';
                
                
                tklist.add(tasks);
   */
     }
        if(!test.isRunningTest()){
            if(mails.size() > 0){
                Messaging.sendEmail(mails); 
            }
            if(tklist.size() > 0){
                insert tklist;
            }
            
        }
        
    }
   }
    global void finish(database.BatchableContext bc){
        
    }
    global void execute(SchedulableContext sc) {
        Lead_Escalation b2 = new Lead_Escalation();
        ID batchprocessid = Database.executeBatch(b2,50);  
    }
    
}
 
Team NubesEliteTeam NubesElite
Hi Priya,
Please check this code it's working fine
global class Lead_Escalation implements database.Batchable<sobject>,schedulable{
    global database.QueryLocator start(database.BatchableContext bc){
       
         integer day=system.today().day();
         string  query;
            string a ='Website' ;
         string b ='Phone' ;
         if(!test.isRunningTest()){
             query='select id,Name,Owner.ID,Assigned To,activityid,owner.firstname,owner.lastname,Owner.Email,LeadSource from lead where activityid=null AND (LeadSource=:Website OR LeadSource=:Phone) order by id';
            }else{
                query='select id,Name,Owner.ID,owner.firstname,owner.lastname,Owner.Email,LeadSource from lead limit 1';
            }
            return database.getQueryLocator(query);
            }
        global void execute(database.BatchableContext bc,list<lead>ldlist){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        list<task> tklist=new list<task>();
        for(lead l:ldlist){
                string body= 'Dear '+' '+'l.owner.name'+'l.name'+'\n'+
                    'It has been two hours since this lead is created. Please contact the customer';
            {  
                   Messaging.SingleEmailMessage message= new Messaging.SingleEmailMessage();
                message.setSubject('Reminder :Enquiry for '+'l.name' + ' to be acted on.');
                List<String> sendTo = new List<String>();
                sendTo.add(tklist[0].Owner.Email);
                  message.setToAddresses(sendTo);
                message.setBccAddresses(new string[]{'priyaaakanksha28@gmail.com'});
                message.setsenderdisplayname('NN');
                message.setSaveAsActivity(false);
                message.setHtmlBody(body);
                mails.add(message);
                
                
               task tasks=new task();       
                tasks.WhoId=l.id;
                tasks.OwnerId=l.ownerid;
                
               tasks.Subject='Reminder';
               tasks.Description='Dear '+' '+'l.owner.name'+'l.name'+'\n'+
                    'It has been two hours since this lead is created. Please contact the customer';
               tasks.TaskInfo__c = 'alert';
               tasks.Priority='Medium';
               tasks.Status='Open';
               tasks.AssignedTo ='Owner';
               tklist.add(tasks);
     }
        if(!test.isRunningTest()){
            if(mails.size() > 0){
                Messaging.sendEmail(mails);
            }
            if(tklist.size() > 0){
                insert tklist;
            }
            
        }
        
    }
   }
    global void finish(database.BatchableContext bc){
        
    }
    global void execute(SchedulableContext sc) {
        Lead_Escalation b2 = new Lead_Escalation();
        ID batchprocessid = Database.executeBatch(b2,50);  
    }
    
}



Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this as solution if your problem resolved.