9 votes

Accessing Non-Static Variables in Static Methods of a Visualforce Controller

The short answer is quite simply, you cannot get to the instance of the Apex class that was invoked on page load. It is gone. You are bumping up against one of the architectural peculiarities of ...
user avatar
  • 19.1k
9 votes

keeping a running count (rollup) with batch apex

To do this you would want to implement Database.Stateful. There's a great example of how to do this in the documentation global class SummarizeAccountTotal implements Database.Batchable<...
user avatar
8 votes
Accepted

Batch finish method getting null values

From the documentation: Using State in Batch Apex Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed ...
user avatar
  • 142k
7 votes
Accepted

Batch performance impact of Database.Stateful

Obviously Growing data in your stateful collections may lead to hit heap limits. A batch class as a whole can process 50 million records, 10,000 records per batch and only 10,000 batches per 24 hrs ...
user avatar
  • 160
6 votes

Why do static and non-static lists behave differently in batch and test contexts?

Static variables are wiped out between transactions; since a unit test is a single transaction, the static variable persists during a unit test, but not when ran as live code. The local variable is a ...
user avatar
  • 430k
6 votes
Accepted

Batch Apex - Static List not available in finish method

In batch jobs, static variables don't maintain state - they are re-set in each transaction. If you want to maintain state and access it in the finish method use member variables. Here's the relevant ...
user avatar
5 votes
Accepted

Batch - Sending data from one batch to another

Should I implement Database.Stateful at this case? I know Database.Stateful used for passing data between executing method of the same batch. for example, counting or sum a field. Of course if the ...
user avatar
  • 61.4k
5 votes
Accepted

Schedulable equivalent of Database.Stateful?

For Schedulable, if you need to maintain state, you can always use System.abortJob and System.schedule. Namely, you can use "this" to reschedule yourself. Here's a simple implementation that simply ...
user avatar
  • 430k
4 votes

How do I pass an external variable into batch Apex?

To pass values from one batch class' s finish() to another's constructor you would have: First batch class global class FirstBatchClass implements Database.Batchable<SObject>, Database....
user avatar
  • 61.4k
3 votes

How Can A Batch Transaction Be Executed From A Nested Loop To Update Variables In The Outer Loop?

You can't. It's not possible to share variable access across transaction boundaries. Database.Stateful allows you to preserve state from one batch invocation into the next, but it doesn't allow live, ...
user avatar
  • 86k
3 votes

Database.Stateful does not work as expected when using instance variable of another class

In documentation we have an example with private final String initialState; variable. This batch class does not implement Database.Stateful. Maybe you should just remove final keyword because we ...
user avatar
2 votes
Accepted

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

You need to save your DML statements until after you've done all your callouts. Ideally, this means tout need to bulkify your method so it can accept a list of wrapper classes. Here's a roughly ...
user avatar
  • 430k
2 votes
Accepted

Getting error in batch apex on runtime

When declaring variables you can initialize them in the declaration. You attempted to initialize them in the testbatch() method but you never called it so that variables / properties were always null ...
user avatar
  • 53.1k
2 votes

Database.Stateful does not work as expected when using instance variable of another class

There might be specific known issue in specific version of Salesforce API if issue occurs on Production but isn't seen in Sandbox. For example, if your production has winter 19 release and sandbox ...
user avatar
  • 13.2k
2 votes
Accepted

Dynamic IF condition check

You could do this without any specific matches in an If-statement. Just have a map that maps state names to maps with your records. Assuming you want Accounts in your mapped maps: List<Account> ...
user avatar
2 votes
Accepted

Database.Stateful in Batch gives errors

global List<Sales_Data_Aggregated__c> listSalesDataAggregated_Upsert = new List<Sales_Data_Aggregated__c>(); global List<Sales_Data_Aggregated__c> listSalesDataAggregated_Delete = ...
user avatar
  • 430k
1 vote
Accepted

transient List of Database.Error becomes null in finish

Transient variables are not serialized, and serializing some types of objects throws exceptions because they are not serializable. You'll need to convert the Database.Error objects into something you ...
user avatar
  • 430k
1 vote
Accepted

How to check for the heap-size limit in batch apex?

You can check if you're near the limit with something like: if(Limits.getHeapSize() > 0.9*Limits.getLimitHeapSize()) { This puts you at 90% or more usage, typically a safe value, but you may want ...
user avatar
  • 430k
1 vote

How update 120,000 Contacts which must be sorted in a specific order

One option to consider is implementing a "Chained Job" that allows the jobs to be run one after another by starting the second job as soon as the first job has finished (and so on). The ...
user avatar
  • 358
1 vote
Accepted

Batch Generated CSV attachement is cut

My suspicion is that you have an issue with cell values that include embedded delimiters (looks like you are using ; or line breaks or double quotes. Best practice is to use String class method ...
user avatar
  • 61.4k
1 vote
Accepted

Stateful Batch Apex with logic outside of the execute() method

You need to remove the static keyword. It basically means "stateless", which is the opposite of what you want. Works public void doWork() Fails public static void doWork()
user avatar
  • 142k
1 vote

How do I maintain state for Dom.Document type variables between Start and execute method of a batch

As you have found, transient does not make your error go away. You just stop tracking that state. It does not make any sense to use transient variables in a Stateful batch. Is it really state that ...
user avatar
  • 142k
1 vote

Batch - Sending data from one batch to another

Since you have many questions, I will answer one for now. Should I implement Database.Stateful at this case? Well, it depends on what you actually need. If you want to use mapValues in all execute ...
user avatar
  • 4,645
1 vote
Accepted

Sending data from one batch to another with amount of data

First of all it would be good to remember that adding Stateful interface to the batch drastically decreases its performance. If you create mapping based on records (and it seems so) it is probably ...
user avatar
  • 62
1 vote
Accepted

Passing a set of record Ids from a Stateful batch class to another batch class

Ok, so I kept on trying stuff and fixed the problem by removing the static modifier from my global id sets. The start of RecalculateTaskRollupBatch now looks like this (and it works): global class ...
user avatar
  • 1,194
1 vote

Database.Stateful and Database.SaveResult in a Batchable class giving internal errors

You need to contact Salesforce and tell them you got an error at this time.., or cause the error to happen again so you can get an exact time.. They will be able to see the error that is actually ...
user avatar
  • 4,327

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