@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){
}
}
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:
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
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:
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.
@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){
}
}
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 orfour Lakh Fifty Thousand rupees
Hi Rameshder,
Glad to hear that !
Here is your Test Class Code :
And also modify your batch with the following code :
Please let me know if this helps !
Thanks,
Apoorv
All Answers
Please try below code to schedule
Please let me know if this helps.
Thanks,
Apoorv
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.
Here you go:
Scheduler class :
Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
Please mark this question as solved, if this helps you.
Thanks,
Apoorv
This is my batch apex class How to schedule this class every day at 11 15
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 :
Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
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
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
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 :
Please let me know if this helps you.
Thanks,
Apoorv
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.
NW i am stuck in test class
Hi Rameshder,
Glad to hear that !
Here is your Test Class Code :
And also modify your batch with the following code :
Please let me know if this helps !
Thanks,
Apoorv
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