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
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
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
6 votes
2 answers
2k views

Getting CalloutException when testing Batchable class with Callouts and HttpCalloutMock

I'm trying to write test code for a Batchable class that AllowsCallouts. I've written a lot of other test code that uses HttpCalloutMock classes, and planned to do so for this class as well. And, ...
tomlogic's user avatar
  • 4,461
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
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
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
1 vote
1 answer
457 views

Which one is the Optimize one when consider in a Batch?

Which one is the best way to optimize the performance if optimization available, Option 1: public without sharing class AccountTrigger extends TriggerHandler{ List<Id> accountIds; public ...
Hariprasath's user avatar
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
  • 491k
9 votes
2 answers
1k views

Why aren't iterators in batch jobs lazy-loaded?

I have a batch job that needs to callout to an external web service and retrieve a variable number of records. Seems like a perfect use-case to have an iterator which performs the callouts until there ...
Greg Grinberg's user avatar
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
19 votes
2 answers
738 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
  • 491k
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
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
9 votes
1 answer
2k views

Batch Processing Quick Query

I have like 50000 records that meet my condition for batch processing. My batch process begins at say 10:01 AM and ends at 10:10 AM. I set my batch size to be 200. Can someone tell me what would ...
user25311's user avatar
  • 599
6 votes
2 answers
12k views

Calling future method from batch class?

I got a situation where i need to invoke future method from batch class. And i know that it is not possible to do. But there might be some workarounds to achieve it, right? And i dont want to invoke ...
College Management's user avatar
3 votes
2 answers
2k views

Avoid Lock Row exception in Batch Processing

I have a Batch on Lead that is scheduled to run every 5 minutes. The criteria to pick the record in Batch is a field on lead ex. Flag = True. let's say very first-time batch runs and there are 50k ...
Sarvesh's user avatar
  • 998
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
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
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
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
10 votes
1 answer
3k views

Is using Database.executeBatch from a trigger an anti-pattern?

In a managed package we have a trigger on Account that propagates a custom field status value from the Account to a large number of related custom objects and wrote the logic in a batchable to avoid ...
Keith C's user avatar
  • 136k
9 votes
3 answers
6k views

Cascading batch jobs

I want to run 2 batch jobs in series i.e. 2nd batch should start running just after 1st batch finishes. Can I do that?
doga's user avatar
  • 4,575
8 votes
2 answers
3k views

Apex CPU Time limit in Batch start() - is this a Query Timeout and how to cope with?

I have a Batch iterating over a large amount of records (with many fields) and 2 of their related child records. The start() method uses Dynamic SOQL to fetch them via QueryLocator to vary which ...
Robert Sösemann's user avatar
8 votes
1 answer
15k views

Error: Future method cannot be called from a future or batch method

I have a managed packaged that I recently updated and it threw an error for a future method I have that sends information to a third party. The error I received was: Future method cannot be called ...
J.koh's user avatar
  • 189
7 votes
2 answers
7k views

How to handle UNABLE_LOCK_ROW errors when this is caused by a batch APEX job

we are facing a serious issue. We have a batch APEX job running daily which makes updates on records fulfilling certain conditions. There is another job running parallel to this, in some cases ...
user avatar
6 votes
6 answers
35k views

Is it advisable to call Batch from trigger

I was thinking of calling a batch class from my trigger. Is is advisable to do that? As batch will have asynchronous execution with respect to trigger.
SEuser's user avatar
  • 1,855
5 votes
1 answer
2k views

Total number of Batch jobs decreases during execution. Why that?

A customer of my managed package is experiencing a long running batch job to change the number of total jobs during the run? How can that be? Is this an internal optimization at Salesforce? Or ...
Robert Sösemann's user avatar
5 votes
2 answers
8k views

Most robust way to parse CSV file via Apex code

I have been tasked to create a batch apex loader which parses a CSV file from an email attachment. I will use an email handler to get the email with the CSV file attached, and then I will try to load ...
Laureant's user avatar
  • 239
1 vote
1 answer
2k views

Batch Apex Too many SOQL queries: 201

I am getting Batch Apex Too many SOQL queries: 201 when the class is scheduled for run in developer console. Batch Apex: global class Batch_EmailBlast implements Schedulable,Database.Batchable<...
Serena's user avatar
  • 33
0 votes
1 answer
3k views

test class with Batch and Callout [duplicate]

I have a problem with FeedItems in my Test class, it's very weird behaviour Does someone explain to me why this happens? Callout Class public with sharing class Callout { public static void ...
Marcin Kapusta's user avatar
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
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
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
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
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
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
10 votes
3 answers
12k views

Rollback whole Batch on error

I am using an APEX batch to process and insert large amounts of data. Essentially I would like it to process every row to catch any errors with the data and then rollback the entire batch if a single ...
danpker's user avatar
  • 203
10 votes
5 answers
15k views

System.CalloutException: You have uncommitted work pending. Please commit or rollback

I am sending only a single HTTP Request in Batch but still facing error System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out My test code is @...
mathlearner's user avatar
  • 2,151
10 votes
2 answers
3k views

GetContentAsPDF from Batch Apex and Trigger fails with new Update

[Salesforce Update] The critical update named “PageReference getContent() and getContentAsPDF() Methods Treated as Callouts” was scheduled for auto-activation in Winter ’16. To allow for planning ...
RajeshShah's user avatar
  • 1,754
9 votes
1 answer
1k views

Avoiding Mixed DML (setup/non-setup) Approach

I need an approach to allow delegated admins (i.e., non-Administators) to add members to Public Groups and Collaboration Groups (the GroupMember and CollaborationGroupMember objects). I added a ...
newbie's user avatar
  • 298
9 votes
3 answers
3k views

Polymorphism for Batch Processing in Salesforce

Edit: Have edited the title in the hopes that anyone in the future looking for help on a related topic can find this easily. Here is a great way to implement polymophism with batch classes: https://...
coldspeed's user avatar
  • 701
9 votes
3 answers
5k views

Schedule apex class to run every minute as there can only be 5 batch jobs

I have a need to execute the Apex class every minute. I have a Sites VF page and in that I have link called "Export to Excel" which exports bulk data (say 50000 records of my wrapper class type) ...
Sathya's user avatar
  • 3,202
7 votes
1 answer
688 views

Error on Batch Apex for sun.security.validator

I`m making calls to the outbound REST API from Salesforce via Batch apex. Most of the calls are working fine, except in some cases, Im getting below error. For example, if I run 200 batches (with ...
Wendy's user avatar
  • 335
7 votes
1 answer
8k views

How to Search a text in all fields in all objects and return all values?

Can I find a string from all fields from all custom and standard objects and return the data (all field data) of all the records that matches for all the fields of those objects?? Also is there an ...
Sathya's user avatar
  • 3,202
7 votes
2 answers
5k views

Running a Batchable under a different User than the User that called Database.executeBatch

To avoid exceeding the flex queue limit, we would like to chain a batchable, but change the User that the batchable runs under for each new batchable so that its updates are tied to the correct User i....
Keith C's user avatar
  • 136k
6 votes
2 answers
11k views

You have uncommitted work pending. Please commit or rollback before calling out

i have written a future method here is source code @future(callout=true) public static void futureDownloadAttachment(Set<ID> documentsID){ List<SpringCM_Attachment__c> logs = [...
mathlearner's user avatar
  • 2,151
6 votes
3 answers
19k views

Scheduling a Batch job at a particular time

I have a Apex batch class which I would like to schedule at a particular date time. The date time is stored in a variable and changes dynamically. ex Datatime scduleDateTime = datetime.now() ; I want ...
SfdcBat's user avatar
  • 4,791
6 votes
1 answer
6k views

Queueable Job limit hit when trigger is batched

I am trying to run a very simple trigger, in concept. The purpose of the trigger is to count some items simply, then update a field on two objects based on that number. At first, I attempted to ...
Konnor McDowell's user avatar
6 votes
2 answers
2k views

Only query Id field in start() to speed up Apex Batch

I am looking for ways to speed up a complex Batch job which takes days to run in a specific customer org. This Batch is snapshoting records of arbitrary objects into denormalized (as a single records ...
Robert Sösemann's user avatar
6 votes
1 answer
2k views

Sobject type not supported in Managed Package

I have recently created a managed package . To test it, I installed it in a fresh developer org. This package queries the account records and updates certain custom fields(part of the package) on ...
sfdc2013's user avatar