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
25
votes
5answers
32k 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 ...
26
votes
4answers
22k 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....
6
votes
2answers
1k 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, ...
25
votes
6answers
9k 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 ...
14
votes
5answers
18k 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. ...
5
votes
2answers
9k 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 ...
99
votes
1answer
3k 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 ...
15
votes
3answers
26k 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,...
34
votes
10answers
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 ...
14
votes
3answers
20k 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 ...
9
votes
1answer
1k 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 ...
10
votes
2answers
11k 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 ...
9
votes
2answers
671 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 ...
2
votes
2answers
335 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 ...
17
votes
2answers
6k 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 ...
12
votes
2answers
2k 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....
8
votes
3answers
5k 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?
18
votes
2answers
446 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: |---...
9
votes
1answer
2k 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 ...
5
votes
2answers
3k 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 ...
5
votes
6answers
25k 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.
0
votes
1answer
2k 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 ...
5
votes
1answer
999 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 ...
3
votes
2answers
4k 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 ...
16
votes
2answers
35k 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....
8
votes
3answers
4k 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) ...
12
votes
1answer
7k 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 ...
19
votes
3answers
5k 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 ...
11
votes
4answers
3k 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'...
6
votes
1answer
5k 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 ...
12
votes
2answers
11k 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; ...
10
votes
3answers
8k 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 ...
7
votes
1answer
526 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 ...
7
votes
1answer
7k 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 ...
5
votes
2answers
5k views

How do you use Batch for send more than 10 emails?

I have been watching how to implement the batch for sending more than 10 email but I get errors. When I send emails I have a limit 5 and also are sent several times to the same user email global ...
5
votes
1answer
4k views

Recursive functions APEX

I had a use case where i had to traverse through account hierarchy (whose business limit is infinite). So i tried ...
2
votes
3answers
4k views

How do I convert a method to Batch Apex?

hoping I can get some help with Batch Apex as I have zero experience with it and the docs aren't all that good. I have a class that does the following: 1. Constructs a Set of Account Ids 2. ...
10
votes
2answers
2k 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 ...
8
votes
2answers
2k 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 ...
7
votes
3answers
4k views

Scheduled batch “already scheduled”

I had a batch job that I tried to start. Its "startup" method that launches it checks to see if it's already running by querying the asyncjob object, then starts it only if it's not running already ...
6
votes
3answers
12k 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 ...
6
votes
2answers
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 = [...
3
votes
1answer
6k views

Too many query rows: 50001

I am getting Too many query rows: 50001 when i run the batch apex. Below code is part of batch apex (FInish Method). Also there is a soql inside for loop .can someone help me fixing this. ...
2
votes
1answer
3k views

Processing callouts from Batch

I have a tough scenario where a future method should be called from a batch.I came across various articles stating that it is not possible. My scenario is i need to send emails for contacts of account ...
1
vote
1answer
409 views

Best practice for a query inside a batch and Mapping

I'm working on a batch which getting a params from another batch in the finish method. The Params are : A Map - Map<Sobject,Set<String>> A Set - Set<String> I lost the values of ...
0
votes
1answer
96 views

Help with test class for Batch Apex

Below is my callout class which makes two different callouts to external system. In callout 1, it gets a token. In Callout 2, it uses that token to retrieve the actual data. For some reason, I`m not ...
12
votes
1answer
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....
10
votes
5answers
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 @...
9
votes
1answer
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 ...
6
votes
2answers
1k 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 ...