+ Start a Discussion
imishraimishra 

Apex scheduler code

Hi

 

I want to send an email once in every 30 days when the lead rating field in Lead object is warm until it is changed to hot.

 

I am trying to write a apex scheduler class, can anybody please help me with this.

 

Its very urgent.

 

 

Thanks in advance.

Praful GadgePraful Gadge

Yes, Its look simple.

 

  • Create a custom field "isMailSent__c" to check whether we sent a mail to Lead or not 
  • Create an apex class 
global class sendMailToHotLeads implements Database.Batchable<Lead>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String FetchLeads = 'SELECT Id, isMailSent__c, LeadRating FROM Lead WHERE isMailSent__c = false AND LeadRating = \'Hot\'';
return Database.getQueryLocator(FetchLeads);
}

global void execute(Database.BatchableContext BC, List<Lead> listOfLeads)
{
      List<Lead> listOfLeads = new List<Lead>();
      for(Lead objectLead : listOfLeads)//Iterate through Leads
      {
            objectLead.isMailSent__c = true
            listOfLeads.add(objectLead);
      //add code to send mail
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'user@acme.com'};
            mail.setToAddresses(toAddresses);
            String[] ccAddresses = new String[] {'smith@gmail.com'};
            mail.setCcAddresses(ccAddresses);
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('New Case Created : ' + case.Id);
            mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');
            mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
                 'To view your case <a href=https://na1.salesforce.com/'+case.Id+'>click here.</a>');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      }
}

global void finish(Database.BatchableContext BC)
{
//Finish
}
}

 

Hope, this will fulfill the requirement. 

 

Sincerely,
Praful G.

imishraimishra

Thanks for the quick reply.

 

M getting error like---

Error: Compile Error: sendMailToHotLeads: Class must implement the global interface method: Iterable<SOBJECT:Lead> start(Database.BatchableContext) from Database.Batchable<SOBJECT:Lead> at line 1 column 14

 

 

Can you please tell me how to solve this.

imishraimishra

I was able to solve the error.

global class sendMailToHotLeads implements Database.Batchable<SObject>

instead of

global class sendMailToHotLeads implements Database.Batchable<Lead>

 

The error is solved but how to schedule it. When i click on the scheduleApex it shows only classes that implements schedulable but this implements database.batchable.

 

Please let me know is there any other way to schedule it.

 

Thanks.

Praful GadgePraful Gadge

Hi,

 

Please refer http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

Here, see:

global class scheduledMerge implements Schedulable{
   global void execute(SchedulableContext SC) {
      mergeNumbers M = new mergeNumbers(); 
   }
}
  • Here mergeNumber nothing but a batchable  apex class.

After that you can schedule the batch by system.schedule method from running this snippet in developer console as follow

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?'; // adjust choron
system.schedule('Merge Job', sch, m);
  • It will be very helpful for you, if you refer above mentioned link to form chronological expressions.

 

Sincerely,

imishraimishra

Wer should i write the query to select the warm leads here?

Praful GadgePraful Gadge

Here, while scheduling a batch no need to query Leads, as we have already done that in Batch class (Start Method).

imishraimishra
Hi Praful, Its still not working and even i am not getting any error.
imishraimishra
Please see the code as below: This is the batch class: global class sendMailToHotLeads implements Database.Batchable { global Database.QueryLocator start(Database.BatchableContext BC) { String FetchLeads = 'SELECT Id, isMailSent__c, Lead_Ranking__c FROM Lead WHERE isMailSent__c = false AND Lead_Ranking__c = \'Warm\''; system.debug('added lead' + FetchLeads); return Database.getQueryLocator(FetchLeads); } global void execute(Database.BatchableContext BC, List leadList) { List listOfLeads = new List(); for(Lead objectLead : leadList) { objectLead.isMailSent__c = true; listOfLeads.add(objectLead); //add code to send mail Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'ipsita.mishra@wwecorp.com'}; mail.setToAddresses(toAddresses); mail.setSenderDisplayName('Salesforce Support'); mail.setSubject('New Lead Created : ' + lead.Id); mail.setPlainTextBody('Your Lead: ' + lead.Id +' has been created.'); mail.setHtmlBody('Your lead: ' + lead.Id +' has been created.

'+ 'To view your lead click here.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } global void finish(Database.BatchableContext BC) { //Finish } } and thn i have called the batch class from scheduler as below: global class scheduledMerge implements Schedulable{ global void execute(SchedulableContext SC) { sendMailToHotLeads M = new sendMailToHotLeads(); } } Please let me know wat m i missing here.

Praful GadgePraful Gadge
Hi Ip**bleep**a, Update your batch apex as follows: global class sendMailToHotLeads implements Database.Batchable { global Database.QueryLocator start(Database.BatchableContext BC) { String FetchLeads = 'SELECT Id, isMailSent__c, Lead_Ranking__c FROM Lead WHERE isMailSent__c = false AND Lead_Ranking__c = \'Warm\''; system.debug('added lead' + FetchLeads); return Database.getQueryLocator(FetchLeads); } global void execute(Database.BatchableContext BC, List leadList) { List listOfLeads = new List(); for(Lead objectLead : leadList) { objectLead.isMailSent__c = true; listOfLeads.add(objectLead); //add code to send mail Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[]{'ipsita.mishra@wwecorp.com'}; mail.setToAddresses(toAddresses); mail.setSenderDisplayName('Salesforce Support'); mail.setSubject('New Lead Created : ' + lead.Id); mail.setPlainTextBody('Your Lead: ' + lead.Id +' has been created.'); mail.setHtmlBody('Your lead: ' + lead.Id +' has been created. '+ 'To view your lead click here.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } global void finish(Database.BatchableContext BC) { //Finish } } Then, schedule it from developer console as follows: scheduledMerge m = new scheduledMerge(); String sch = '20 30 8 10 2 ?'; // adjust choron system.schedule('Merge Job', sch, m); Now, check in Scheduled Jobs (Setup>Monitoring>Scheduled Jobs) I have checked this in my dev org and its working.
imishraimishra
It is getting scheduled. But m not getting the mails. I have a lead with rating as Warm, but still no mails.
Praful GadgePraful Gadge
Hi,

If the above post is your solution, kindly mark this as the solution to the post so that others may benefit.

Regards,
Praful Gadge.