You need to sign in to do that
Don't have an account?

How to write schedulable apex class to send email on daily basis?
Please help.
I have a requirement to send email on daily basis to a set of people.How do I perform this action?
Please help.
Thanks inadvance.
I have a requirement to send email on daily basis to a set of people.How do I perform this action?
Please help.
Thanks inadvance.
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Apex Scheduler Notes and Best Practices
We use Apex Scheduler to schedule a controller to execute it at a given time in future. For this make an Apex Scheduler Controller and to schedule this controller go to...
Administration Setup->Monitoring->Scheduled Jobs from there we select that Controller class and then provide some time and date to execute it in future.
Below is a sample code of the Apex Scheduler Controller to send an email :
Let us know if this will help you
Thanks
Amit Chaudhary
All Answers
Please check the below code:
Batch Apex
A Batch class allows you to define a single job that can be broken up into manageable chunks that will be processed separately.
When to use Batch Apex
One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you're going to use in this batch context: 'select Id from Account'. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.
Sample Batch Apex
1) Start Method is automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These record are divided into subtasks & passes those to execute method.
2) Execute Method performs operation which we want to perform on the records fetched from start method.
3) Finish Method executes after all batches are processed. Use this method to send confirmation email notifications.
When to use Batch Apex
One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you're going to use in this batch context: 'select Id from Account'. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.
Limits in Batch Apex
1) Up to five queued or active batch jobs are allowed for Apex
2) Cursor limits for different Force.com features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
3) A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed
4) If the start method returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.
5) If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method.Apex governor limits are reset for each execution of execute.
6) The start, execute, and finish methods can implement up to 10 callouts each
7) The maximum number of batch executions is 250,000 per 24 hours
8) Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn’t cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running
Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
1) When you want to process large number of records on daily basis or even on specific time of interval then you could go for Batch Apex
2) Also, when you want an operation to be asynchronous then you could implement the Batch Apex. Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks of data
Please go through the below links:
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBtv
Please do let me know if it helps you.
Regards,
Mahesh
How to check whether this code works or not?
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Apex Scheduler Notes and Best Practices
We use Apex Scheduler to schedule a controller to execute it at a given time in future. For this make an Apex Scheduler Controller and to schedule this controller go to...
Administration Setup->Monitoring->Scheduled Jobs from there we select that Controller class and then provide some time and date to execute it in future.
Below is a sample code of the Apex Scheduler Controller to send an email :
Let us know if this will help you
Thanks
Amit Chaudhary
http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
How to Schedule scheduler class
There are two option we have schedule the scheduler classes.
1) By System Scheduler.
2) By Developer console
System Scheduler.
Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time like below screen shot.
By Developer console
Execute below code from developer console :-
ApexScheduledClass m = new ApexScheduledClass ();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);
Let us know if this will help you
Thanks
Amit Chaudhary
I would like to inform you that, you need to change the above query like on what basis you want to send an email. Once you modify the query, you can open the developer console and execure the below 2 lines of code for testing.
Once the testing is done, you can schedule it using the Salesforce scheduler.
Please do let me know if it helps you.
Regards,
Mahesh
I used ur simple code to send email.
Since it was urgent to test I used developer console to run that code.
There I am getting as success but actually am not receiving any mail.
Please help.
I added hardcoded email id. Please add your email id for test in below line
string [] toaddress= New string[]{'demo@gmail.com'};
Setup-->Monitoring--->Scheduled Jobs
I tesed above code and also got email
Try below code for testing
Sample 1 :-
Sample 2:-
I received the mail.
One more help!
global class ApexScheduledClass Implements Schedulable
02 {
03 global void execute(SchedulableContext sc)
04 {
05 sendmail();
06 }
07
08 public void sendmail()
09 {// Please add your logic according to requirement
10 Messaging.SingleEmailMessage email = newMessaging.SingleEmailMessage();
11 string [] toaddress= New string[]{'demo@gmail.com'};
12 email.setSubject('Testing Apex Scheduler-Subject');
13 email.setPlainTextBody('Testing Apex Scheduler-Body');
14 email.setToAddresses(toaddress);
15 Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
16 }
17 }
What if I want to send one more extra email only on wednesdays along with the existing one how would I implement that inside this class.I have created two Email template.But only on wednesdays I will have to call the second email template.How would I implement that inside this class?
Any idea?
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
System Scheduler.
Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time like below screen shot.
Can someone help me out with the Batch Apex Class and it's test class to generate a report and send through an email in the form of a link
Were you able to figure it out I am currently looking for the same functionality.Any help is appreciated. Thanks
I have a schdulable batch class whih sends mail to the users here problem is as i have scduled the job its taking my email id in To Addresse can any help me how to avoid my mail id from to addresss
thanks
mlm software demo (https://cloudmlmsoftware.com/free-demo)