Questions tagged [batch]

Classes implementing the `batchable` interface are used when there is a need to operate on a large number of data records which would normally be problematic due to the imposed governor limits.

Filter by
Sorted by
Tagged with
103 votes
1 answer
4k views

When are the Queueable/Batchable penalties going to be enforced?

According to The New Apex Queueable Interface, from October 2014, we were told that chained Queueable calls would be exponentially delayed up to 64 seconds, chaining Batches would be exponentially ...
sfdcfox's user avatar
  • 490k
49 votes
3 answers
21k views

Why use Batch Apex?

What is the point of batch apex? I've been trying to do some research but everything I find just tells me how to use it or goes over scenarios where it might be useful - without telling me why I would ...
Ryan Elkins's user avatar
  • 7,677
36 votes
10 answers
2k views

How can I cause side effects outside an execution context?

I've got a scenario that relies on the system's "automatic rollback" behaviour to undo any DML, emails, futures and so on, in the case of an error. Basically, by allowing code to throw an unhandled ...
Matt and Neil's user avatar
36 votes
1 answer
2k views

An interface extending a system interface causes process hangs?

We're trying to implement dynamic batch processes to dispatch different classes. Specifically, we have integrated multiple systems, and depending on certain events, we dispatch callouts to those ...
sfdcfox's user avatar
  • 490k
29 votes
5 answers
19k views

Workaround for the 10 web service callout limit in apex?

What are the workarounds for the 10 web service callout limit in apex. If you need to make, say 50 callouts, how do you batch them so that you can execute them all asynchronously? Is there a good ...
paul's user avatar
  • 1,019
27 votes
4 answers
27k views

Can Queueable solve "Future method cannot be called from a future or batch method"?

I have code that must be called as @future as it requires all the resources one can get. The only problem is that in 1% of the cases this code is called from a Batch and therefore fails with System....
Robert Sösemann's user avatar
26 votes
3 answers
50k views

Database.Stateful. When to use it?

I am writing a batch class which will get the records whose status picklist value is 'no response' and whose created date is before greater than or equal to 48 hrs and update the status to 'declined'. ...
KSR's user avatar
  • 387
26 votes
6 answers
38k views

Calling future method from Batch

I have a requirement where a future method need to be called from batch apex ,I came across various articles stating that a future method could not be called from the batch. Is there any alternate ...
Manoj Chandran's user avatar
25 votes
6 answers
11k views

Scheduled batch jobs and durability

I have a process that runs scheduled Apex quite regularly as a batch job manager. This process runs on a one-time schedule, and looks into a batch job table (custom object). If it finds work to do, it ...
jkraybill's user avatar
  • 11.2k
25 votes
4 answers
2k views

Batch start CPU timeout - What influences start() post-processing?

We're experiencing a very annoying issue with a Batch job processing "Large Data Volume" (1.5M records over a table containing 4.5M records). Our batch start method last statement is: return Database....
MLucci's user avatar
  • 1,098
24 votes
1 answer
2k views

Can we use a single class as a batchable class and install handler?

Background We would like to use a single class for installing our app. To avoid having any more classes than we need, we're trying to consolidate installation into a single class. Class public ...
sfdcfox's user avatar
  • 490k
23 votes
2 answers
61k views

Passing Parameter to batch apex

I need to pass parameter to the batch apex class. This is apex class: global class batchNotesInsert implements Database.Batchable<sObject> { global Database.QueryLocator start(Database....
Jana Shree's user avatar
22 votes
4 answers
33k views

AsyncApexExecutions Limit exceeded error for apex batch

I am getting "AsyncApexExecutions Limit Exception" in below code - Can anyone explain the issue? i have been getting more than 1 lakh records in start method using queryLocator but didn't get this ...
Pramod Kumar's user avatar
  • 5,300
22 votes
1 answer
5k views

Is finish() always called when Batch is manually aborted?

Is the finish() method of an Apex Batch class called when a user aborts the batch during startup or processing? Is there a way to find out who aborted it?
Robert Sösemann's user avatar
22 votes
2 answers
9k views

What are the implications of implementing Database.Stateful?

I've recently implemented a batch apex class that initially did not implement Database.Stateful. After using it for a while as-is I decided to implemente better error handling via a simple Map to ...
ca_peterson's user avatar
21 votes
5 answers
77k views

How can I query the class which the schedule job will call

I am trying to get the name of the schedule / batch classes called by the schedule job Trying this select Id,CronJobDetailId,CronJobDetail.Name, NextFireTime,PreviousFireTime,State,StartTime,...
Michael Gill's user avatar
  • 2,202
21 votes
1 answer
17k views

How to run a batch class for list of string

I have a requirement where I need to process a list of string having hundreds of records in it . For each string I need to have an http callout so I cant run in a normal apex method. For this I am ...
Shebin Mathew's user avatar
20 votes
2 answers
7k views

are Triggers (and their delegated classes) or Batch affected by sharing?

I understand with sharing classes run in User Mode, meaning they respect permissions, field-level security and sharing rules of the current user. I understand without sharing classes run in System ...
Matt and Neil's user avatar
20 votes
2 answers
6k views

Why does Database.QueryLocator give better permormance than the Iterable<sObject> in Batch jobs

I observed that if i use Database.QueryLocator in the batch start method than for the Iterable<sObject> I get better performance. However I could not figure out why. In my case Iterable<...
Pramod Kumar's user avatar
  • 5,300
20 votes
3 answers
5k views

Cool, we can chain batches now! But we can't test them. Do we?

I just used the new feature that allows me to call a SecondBatchable class from the finish() method of the FirstBatchable class. global with sharing class FirstBatchable implements Database....
Robert Sösemann's user avatar
20 votes
1 answer
3k views

Batch performance impact of Database.Stateful

I know that a Batch will generally run faster when the Database.Stateful marker interface is not used. What I don't know is, how badly the serializing and deserializing of state between each execute ...
Robert Sösemann's user avatar
19 votes
3 answers
14k views

Why does the value of Static variables in batch context reset for each transaction

As part of the batch context, the static variables get reset at each transaction irrespective of Stateful/Stateless. class batch_test implements database.batchable, database.stateful () { static ...
Sithlord's user avatar
  • 191
19 votes
2 answers
737 views

Can batchable custom iterables support null values?

Background We're trying to do a type of pseudo-parallel processing mechanism, where a record will be processed twice. The thought was to make an overlapping series of values that look like this: |-----...
sfdcfox's user avatar
  • 490k
18 votes
3 answers
7k views

How to test that Apex Batch finish() sends an email?

I have a Batch class that sends an Email from its finish method and want to test that. a But....I cannot test it. The the above test fails as there was no email invocation at all. BUT: I see it in the ...
Robert Sösemann's user avatar
16 votes
5 answers
33k views

How to test a scheduler which runs a batch?

I have a scheduler class which calls a batch Apex. I want to write an Apex test for the scheduler class. However, the test fails. It seems that the testing framework does not execute the batch class. ...
Riadh Mankai's user avatar
  • 1,088
16 votes
2 answers
11k views

What is the purpose of Global and Final modifiers in Batch Apex?

I've noticed that Batch Apex classes are all global, and the dev console returns an error if you just try to do a public/private class. My understanding is that a global class is visible to all ...
smohyee's user avatar
  • 3,816
16 votes
3 answers
21k views

Best practices for monitoring Scheduled Apex and Batch Apex?

We have multiple Apex jobs running, both from Schedulable and Batchable code. While an attentive SA could examine the Apex Jobs list to check that everything is OK, it seems a better idea to have ...
Keith C's user avatar
  • 136k
16 votes
2 answers
3k views

Does Test.stopTest() ensure a System.Schedule/Database.Batchable completes in a test?

I am struggling to successfully test some code that is essentially the same as the code here Apex test class for schedulable class that checks for 5 batches running already. I can get the System....
Keith C's user avatar
  • 136k
15 votes
2 answers
38k views

Scheduling batch Apex

Hi this is my batch apex code global class batchAUpdate_based_on_stage implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { ...
Sharat's user avatar
  • 1,029
15 votes
2 answers
14k views

NO_MASS_MAIL_PERMISSION Error from Unit Test on SingleMailMessage

I am currently working in a Config-Only sandbox. I have a unit test that is continually failing with the following error message: System.EmailException: SendEmail failed. First exception on row 0; ...
Jesse Altman's user avatar
  • 19.7k
15 votes
1 answer
12k views

Using Batch Apex on millions of records

Part of our app behaviour is going over millions on records and updating their data. From what I read, we can do it using Batch Apex although I am not certain I fully understand how it works. Here ...
Uri Lukach's user avatar
  • 1,264
15 votes
1 answer
1k views

Enums as Map keys don't work in Batchable

I'm running into some weird behavior when using an Enum as a Key in a map. This same code works fine in other contexts, but not when in batch. I have an Enum to represent currency types: public ...
NSjonas's user avatar
  • 10.2k
15 votes
1 answer
4k views

Testing BatchApexErrorEvent trigger

I have a batch class which implements the Database.RaisesPlatformEvents interface. This interface has recently been introduced and helps log unexpected exceptions: https://developer.salesforce.com/...
Koen Wesselman's user avatar
15 votes
1 answer
2k views

Detect if Apex Flex Queue is enabled in an Org

I have a managed package that starts a batch job. Right now, before attempting to start the batch it does a check like: integer batchJobLimit = 5; integer currentBatchCount = [SELECT count() ...
Daniel Ballinger's user avatar
13 votes
3 answers
3k views

Batch @future calls?

Im trying to build what seemed like a simple SMS messaging application using Twilio's api and running into a few strange momments. So I have an SMS object that stores the messages and i wrote a ...
Keith Mancuso's user avatar
12 votes
1 answer
28k views

No more than one executeBatch can be called from within a test method

This is my test method for batch process: static testMethod void TST_testBatch() { Test.startTest(); testBatch b = new testBatch(); Database.executeBatch(b); Test.stopTest(); } ...
Nagendra Singh's user avatar
12 votes
2 answers
17k views

What does the Batch Apex "Holding" status mean and where is it documented?

I've seen the following values in the AsyncApexJob table during the 'cradle to grave' life cycle of a Batch Apex job: BEFORE running: Queued Preparing WHILE running: Processing AFTER running: ...
Matt and Neil's user avatar
12 votes
1 answer
16k views

Find Apex batch status programmatically

I have a batch class, I need to email and run another batch in the 'Finish' method of batch ONLY IF the batch ran successfully. I would like to have batch status in the finish method (like the batch ...
Mahmood's user avatar
  • 5,984
12 votes
1 answer
4k views

Testing Batch Job with HTTP Callouts

Suppose I have the following: global class CMBatchRegisterUsers implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful { global final String orgID = UserInfo....
1337holiday's user avatar
12 votes
1 answer
3k views

Reason not to implement Database.AllowsCallouts

I have a number of batches all extending a base batch class that handles logging of sorts. Some of these batches do callouts, while others just handle data in Salesforce alone. It happened to me on ...
user254875486's user avatar
12 votes
1 answer
8k views

Apex Batch job limits not clear Queued vs Scheduled

We have a discussion going on, to know now exactly what the limits are on batch apex jobs. The docs are really not clear on this. In this document it states that "Up to five queued or active batch ...
Sven Delporte's user avatar
12 votes
2 answers
1k views

Poll for long-running API responses - Solution patterns

Many REST APIs that people want to use from within Salesforce have asynchronous, long-running operation. For example An endpoint POST /api/resource to create a resource returning a handle Id to check ...
Robert Sösemann's user avatar
11 votes
3 answers
48k views

Error "Too many queueable jobs added to the queue: 2"

I've been looking for similar questions, but none of them applies to my scenario, so I'd be grateful if you can explain me why my logic is failing, and what can I do in order to fix it. I want to run ...
user3529582's user avatar
11 votes
2 answers
17k views

Apex cron job command to schedule an apex job to run every 4 hours daily

I have a requirement where i need to run an apex batch job every 4 hours daily monday to friday and all months in a year. I looked at the apex cron job documentation to be setup using code snippets in ...
Buyan Thyagarajan's user avatar
11 votes
2 answers
5k views

Catching Limit Exception: "Attempted to schedule too many concurrent batch jobs"

I've got a Visualforce page that allows the user to fire a batch job manually, using this action method: public void FireBatch() { try { Database.ExecuteBatch(new AutobotBatch()); ...
Matt Lacey's user avatar
  • 25.6k
11 votes
4 answers
4k views

Viable design for multiple batch jobs

I've recently run across multiple needs to execute multiple different batch jobs at the same time, and almost immediately hit the (somewhat inexplicable) SFDC limit of 5 batch jobs queued at once. I'...
jkraybill's user avatar
  • 11.2k
11 votes
1 answer
621 views

Workaround to change Batch class to use QueryLocator

In my haste to get a new batch class running I accidentally packaged it such that the start() method returns an iterable (List<SObject>) instead of a Query Locator. Bad developer, no Twinkie. ...
Matt Lacey's user avatar
  • 25.6k
11 votes
1 answer
7k views

generating PDF of a record's printable view in batch apex

Some of my users cannot view the Case object owing to restricted licenses. To avoid paying for service cloud licenses, I need to generate PDF snapshots of all Case records using a batch (the are many ...
max's user avatar
  • 2,419
11 votes
2 answers
2k views

Failure to send email causing a database rollback

I'm having a strange issue within Salesforce. I am trying to create an object and send an email about it on the finish() command on a "Database.Batchable, Database.AllowsCallouts, Database.Stateful". ...
Mattisdada's user avatar
  • 1,071
11 votes
3 answers
2k views

Measuring performance of batch apex query

Is there a good way to tell how long the query in a batch apex job is taking? I don't find anything in the debug logs. I can see each batch and how long it takes, but not how long the initial query ...
mscholtz's user avatar
  • 1,324

1
2 3 4 5
41