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
RameshderRameshder 

Hi Guys I am stuck In Schedulin Class?

How to schedule a class For every Morning at 11-15 I want to send one Email Tempalte for 1 user?

How to schedule pls help me guys
Best Answer chosen by Rameshder
Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder, 
 

Glad to hear that !

Here is your Test Class Code :

 

@isTest
public class sendEmailToContactsTest{
    
    static testMethod void sendMailTest(){
        Account acc=new Account(Name='Test Account');
        insert acc;
        Contact con = new Contact(LastName='Test',Email='test@gmail.com'); // specify all the fields that matches you criteria.
        insert con;
        sendEmailToContacts ob = new sendEmailToContacts();
        Database.executeBatch(ob,1000);
        
    }
 
}
 

And also modify your batch with the following code :

 

global class sendEmailToContacts implements Database.Batchable<sObject>{
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select id, Name,Email from Contact where Email=\'test@gmail.com\''); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Contact> conList){
        EmailTemplate et=new EmailTemplate();
        if(!test.isRunningTest()) et = [SELECT id FROM EmailTemplate WHERE developerName = 'case_closing'];
        
        List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
        for(Contact con:conList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(con.id);
            if(!test.isRunningTest()) mail.setTemplateId(et.id);
            if(test.isRunningTest()) mail.setHTMLBody('Test');
            mail.setSaveAsActivity(false);
            mail.setSenderDisplayName('Your Name');
            mail.setReplyTo('Your Email');
            msgListToBeSend.add(mail);
        }
        Messaging.sendEmail(msgListToBeSend);
    }
    
    global void finish(Database.BatchableContext BC){      
        
    }
}
 

Please let me know if this helps !

Thanks,
Apoorv
 

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Rameshder,

Please try below code to schedule 
 
SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 15 11 * * *';
System.schedule('Testing', cron, obj);



Please let me know if this helps.

Thanks,
Apoorv
Dilip_VDilip_V
Ramesh,

Create a batch class and schedule it using below cron expression.

scheduledMerge m = new scheduledMerge();
String sch = '0 15 11 * * ?';
String jobID = system.schedule('Merge Job', sch, m);

Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.
RameshderRameshder
Apporva Please send me full Code if u dont mind...Because I am new to salesforce
Apoorv Saxena 4Apoorv Saxena 4
Hi Rameshdar,

Here you go:

Scheduler class :
 
global class SampleSchedulableClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      YourBatchClassName bp = new YourBatchClassName();  // Create a instance of your Apex Batch class that you want to execute
   }
}

Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
 
SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 15 11 * * ?';
System.schedule('Testing', cron, obj);


Please mark this question as solved, if this helps you.

Thanks,
Apoorv

RameshderRameshder
global class SendingRemainderToTarun implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT id,Email FROM Contact  ';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
       
    }   
    
    global void finish(Database.BatchableContext BC) {
    
     

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
Contact recipient = [SELECT id, firstname FROM Contact where Email = 'test@gmail.com'];
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'case_closing'];
 
mail.setSenderDisplayName('Your Loving Administrator');
 
mail.setTargetObjectId(recipient.id); // Specify who the email should be sent to.
mail.setTemplateId(et.id);
 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

    }  
}

This is my batch apex class How to schedule this class every day at 11 15
Apoorv Saxena 4Apoorv Saxena 4


Hi Rameshder,

You are querying on Contact object and in your execute method you are accepting List<Account> as a parameter?

Anyways to schedule your batch job , please follow the steps below:

Create a new Apex Class with the following code :

Apex Scheduler class :
 

global class SampleSchedulableClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      SendingRemainderToTarun bp = new SendingRemainderToTarun();  // Creating a instance of your Apex Batch class that you want to execute
   }
}



Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
 
SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 15 11 * * ?';
System.schedule('Testing', cron, obj);

Now to check when your batch job is scheduled to run. Type 'Scheduled Jobs' in Quick Find Box and click on 'Scheduled Job' under Monitor section.You will be able to see when you batch job is scheduled.

Please mark this question as solved, if this helps you.

Thanks,
Apoorv

 
RameshderRameshder
Hi apoorv I want to send that Email template to particular email id 
RameshderRameshder
How to schedule that class every Day at 11 15 AM
Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder,

 

I have answered this question here : https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XbRoQAK

Please mark both the threads as solved if this resolves your issue.

Thanks,

Apoorv

RameshderRameshder
Apoorva Its schedulig but i am nt receving any email or template to my email id
Apoorv Saxena 4Apoorv Saxena 4

Check your Email Deliverability. 

Type 'Deliverability' in Quick Find box and Set its access level to 'All Emails' and click 'Save'. 

Please refer to screenshot below :

User-added image

Please let me know if this helps you.

Thanks,

Apoorv

RameshderRameshder
User-added image
RameshderRameshder
I enabled still a am nt yet receive apporva....
Apoorv Saxena 4Apoorv Saxena 4
Can you show me a screenshot of Scheduled Jobs ?
RameshderRameshder
User-added image
Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder,

Type 'Apex Jobs' in Quick Find Box and check the Status of your batch job. Is it Completed ?

Do post a screenshot for reference.

RameshderRameshder
Hi Apporva Its working now....Thank u so much...
NW i am stuck in test class
Apoorv Saxena 4Apoorv Saxena 4

Hi Rameshder, 
 

Glad to hear that !

Here is your Test Class Code :

 

@isTest
public class sendEmailToContactsTest{
    
    static testMethod void sendMailTest(){
        Account acc=new Account(Name='Test Account');
        insert acc;
        Contact con = new Contact(LastName='Test',Email='test@gmail.com'); // specify all the fields that matches you criteria.
        insert con;
        sendEmailToContacts ob = new sendEmailToContacts();
        Database.executeBatch(ob,1000);
        
    }
 
}
 

And also modify your batch with the following code :

 

global class sendEmailToContacts implements Database.Batchable<sObject>{
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select id, Name,Email from Contact where Email=\'test@gmail.com\''); // you can modify the query as per your requirement.
    }
    
    global void execute (Database.BatchableContext BC, List<Contact> conList){
        EmailTemplate et=new EmailTemplate();
        if(!test.isRunningTest()) et = [SELECT id FROM EmailTemplate WHERE developerName = 'case_closing'];
        
        List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
        for(Contact con:conList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(con.id);
            if(!test.isRunningTest()) mail.setTemplateId(et.id);
            if(test.isRunningTest()) mail.setHTMLBody('Test');
            mail.setSaveAsActivity(false);
            mail.setSenderDisplayName('Your Name');
            mail.setReplyTo('Your Email');
            msgListToBeSend.add(mail);
        }
        Messaging.sendEmail(msgListToBeSend);
    }
    
    global void finish(Database.BatchableContext BC){      
        
    }
}
 

Please let me know if this helps !

Thanks,
Apoorv
 

This was selected as the best answer
RameshderRameshder
How to convert Integer to string
Apporva i am stuck in another  requirement in Visual force Page
I am entering Salary amount in Contact Object [Example 450000]
i am fetching that salary value in render pdf In pdf I want to display 4 lakh 50 thousand or four Lakh Fifty Thousand rupees