Is there anyway to maintain the state of a certain variable in Batch Apex across transactions. Database.Stateful will save the state for all the declared variables, Can i set it for only one specific variable?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
For the variables in stateful class in whom you don't wanna store state, you can make those variables as transient.
Use the transient keyword to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce page. You can also use the transient keyword in Apex classes that are serializable, namely in controllers, controller extensions, or classes that implement the Batchable or Schedulable interface
You need to implement the Database.Stateful interface and declare an instance variable to keep its state. If you declare it as static, it doesn't maintain its state across batch jobs.