24

Just saving the class does not schedule it. You still need to tell the DB to schedule it. I generally write a schedule function within my class so that I can easily call it in one line, that uses my static chron string within the class. Something like this should work global class Scheduler_class implements Schedulable{ public static String sched = '...


22

Answers to your concerns : 1)Yes batch apex are asynchronous process and can support upto 50 million records and hence for 1 million this code will run 2)You will specify batch size while you invoke batch .Say i specify batch size as 400.Then 50000/400 number of batches will run.In each batch context 400 records will picked . 3)No there are no time outs ...


22

I had a similar problem before which I solved by explicitly calling the execute method of my Schedulable class. Try something like this: Test.startTest(); AccountBatchScheduler abs= new AccountBatchScheduler(); String jobId = System.schedule('myJobTestJobName', cronExpr, abs); abs.execute(null); Test.stopTest();


21

If you use the following chron string for your scheduled job, it will run every 4 hours (at set times) Monday through Friday. apexScheduledJob j = new apexScheduledJob (); String sch = '0 0 0,4,8,12,16,20 ? * MON-FRI'; System.schedule('My Job', sch, j); If you wanted it to run every day, you would just change it to String sch = '0 0 0,4,8,12,16,20 ? * ...


21

For the first line of your class, use this: global class ClassName implements Database.Batchable, Schedulable, Database.AllowsCallouts {


17

Ok, found what I was looking for select ApexClassId, Id, JobItemsProcessed, JobType, Status, NumberOfErrors, MethodName from AsyncApexJob where JobType in ('BatchApexWorker','ScheduledApex')


16

After finally speaking to someone in Salesforce support who seemed to know what my point was, it was explained to me: 'Actual execution might be delayed based on service availability' means that the batch is enqueued for the interval initially set from the scheduleBatch method. 'Service availability' is not a reference to the concurrent batch count ...


14

Here's the problem from the docs: When testing your batch Apex, you can test only one execution of the execute method. You can use the scope parameter of the executeBatch method to limit the number of records passed into the execute method to ensure that you aren't running into governor limits. Additionally I don't think batch classes are executed from ...


13

Why not just perform the callout in your Batch Apex start method? This means you will always start the Batch Apex job from your Scheduled apex job, but i think is on balance better than having to marshall schedule jobs between @future jobs? public with sharing class BatchWithCallout implements Database.Batchable<SObject>, Database.AllowsCallouts {...


13

We can have 100 scheduled Apex jobs at one time. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm Things to consider: Scheduled APEX are like Planes. They can be on-time or little late depending on Queue. Scheduled Jobs execute as Synchronous Jobs, meaning it would get limits which are respective to Synchronous Jobs(6MB ...


13

You can check the AsyncApexJob.Status using the JobId from the Database.BatchableContext. E.g. global void finish(Database.batchablecontext bc) { // Check batch status - IF COMPLETED then AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus from AsyncApexJob where ...


13

As you are chaining the jobs you have limit of 1 on child jobs. When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job isn’t supported. See here under queuable Apex limit section: https://...


12

Thanks for posting the question, this is a weird one. As the Salesforce documention says, Test.stopTest() should force asynchronous processes to run. It works for Apex Batch on its own, and it works for Schedulable on its own. But when you have a Schedulable that runs an Apex Batch, it doesn't behave as expected: It looks like Test.stopTest() does force the ...


12

For daily 10 PM 0 0 22 * * ? * first 0 for seconds second 0 for min third 22 for hours i.e. 10 PM you can use http://www.cronmaker.com/ for generating cron expression for more detail check Apex Scheduler


11

No. Execute will not be called unless at least one non-null item is available for processing. You'll have to come up with an alternative method. As a corollary to this statement, "scope" will never be empty or null, because execute won't be called if there's nothing to do.


10

Yes, you can start a batch from a trigger, but I would advise against it. From the documentation: Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger will not add more batch jobs than the five that are allowed. In particular, consider API bulk updates, import wizards, mass record ...


10

Take a look at the CronTrigger options using the System.Schedule method (halfway down the page): http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm Based on this, you can see that you are able to choose both the month and day of the month. You'll need to either run this Apex snippet via Anonymous Apex (the GUI for scheduling ...


9

Adding an answer because I always end up here and then have to look through the docs to remember the syntax: Cron Format: second minute hour day_of_month month day_of_week optional_year Format Values second: 0 (this doesn't actually do anything) minute: 0–59 hour: 0-23 day_of_month: 1-31 - * ? / L W month: 1-12 JAN-DEC , -...


8

What you will have to do is check the number of running jobs, I have built my own pattern to do this and here is an excerpt of code from that: private void submitJob( IReschedulable job, SchedulableContext SC ) { if( getCurrentJobCount() > 4 ) { // try again in a minute Datetime sysTime = System.now().addSeconds( 60 ); ...


8

One solution that was mentioned in the comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below In my Scheduled Class global void execute(...


8

The presence of the class alone will not cause it to make its way into any schedules. I think you need to run Database.Schedule from execute anonymous (eg in Developer Console). Database.schedule('My First Schedule', '0 1 1 * * ?', new Scheduler_class())


8

Yes. In general, you can combine as many interfaces as you want within a single class, so long as they are all compatible. Using the scheduable and batchable interface within the same class just means you have to implement all four methods. Your class will start off like: public class x implements Database.Batchable<Object>, Database.AllowCallouts, ...


8

You need to transform your datetime to the schedule format. More info here Example String day = string.valueOf(system.now().day()); String month = string.valueOf(system.now().month()); String hour = string.valueOf(system.now().hour()); String minute = string.valueOf(system.now().minute() + 1); String second = string.valueOf(system.now().second()); String ...


8

Not sure this is worthy of an answer, but too much to put as a comment. The only time I have seen anything remotely similar was with this and I was able to prove to support that a job had run and created duplicates. The info I got back from Tier 3 at the time: As we discussed, during the execution, an internal server error on the asynchronous process for ...


8

Seems I got some attention after submitting a case or this post or whatever, just got this notification: A bit late but at least I know the cause On March 16, 2017, Salesforce identified a misconfiguration in our test environment that allowed Apex callouts and outbound messages to reach endpoints outside of the test environment. At the same time, ...


8

FURTHER UPDATE: It seems that, whilst there is some sympathy from within Salesforce, the Salesforce PM and R&D team responsible believe this is a "feature enhancement", not the reporting of a bug, because they "already did the analysis" (some 6 years ago). Their main argument is that "there is an existing backlog of features, ...


7

@Phil Hawthorn: I also expected this new method to help us circumvent the Max 5 batch Limit and experienced the same frustration. I party solved this by creating a custom batch queue using database serialization. To invite other to provide feedback and collaborate on such a custom Apex queue I created the GitHub project SObject Work Queue which had the ...


7

Move your callouts before your DML statements. In other words make sure that all update, insert and delete statements in your execute method occur after any callout statements. A quick search of "uncommitted work" will reveal that there are plenty of questions on this site that address this issue Here's an example : Assume you have a queue of "...


7

You can do that with the following cron string. apexScheduledJob myJob = new apexScheduledJob (); String sch = '0 0 0,2,4,6,8,10,12,14,16,18,20,22 ? * *'; System.schedule('My Job', sch, myJob); This job will run every 2 hours, at the top of the hour, starting at midnight.


7

I use this pattern, i've seen lots of others but all do something similar: private static Integer getCurrentJobCount() { return (Integer)[Select count() From AsyncApexJob Where JobType = 'BatchApex' and ( Status = 'Processing' or Status = 'Preparing' )]; } private void submitJob( IReschedulable job, SchedulableContext SC ) { if( getCurrentJobCount()...


Only top voted, non community-wiki answers of a minimum length are eligible