function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sonali  vermasonali verma 

stateful and stateless in batch apex

Hello
what is difference between stateful and stateless?
​Any issues if batch class is executed statelss (without stateful)?

when do we go for stateful and when do we go for stateless?

Please explain​
Best Answer chosen by sonali verma
Amit Chaudhary 8Amit Chaudhary 8

Using Stateful Batch Apex
If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface. This instructs Force.com to preserve the values of your static and instance variables between transactions.

global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{
}
http://salesforceapexcodecorner.blogspot.in/2011/08/state-management-in-batch-apex-in.html

In Short if you need to send a mail to check number of record pass and failed in batch job counter in that case can you Stateful batch job.
If you want to create one counter and share/ use in each execute method use same.


Using Stateless Batch Apex
Batch Apex is stateless by default. That means for each execution of your execute method, you receive a fresh copy of your object. All fields of the class are initialized, static and instance.

global class SummarizeAccountTotal implements Database.Batchable<sObject>{
}

Please let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Using Stateful Batch Apex

Batch Apex is stateless by default. That means for each execution of your execute method, you receive a fresh copy of your object. All fields of the class are initialized, static and instance. If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface. This instructs Force.com to preserve the values of your static and instance variables between transactions.
http://salesforceapexcodecorner.blogspot.in/2011/08/state-management-in-batch-apex-in.html

I hiope this will help u
Amit Chaudhary 8Amit Chaudhary 8

Using Stateful Batch Apex
If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface. This instructs Force.com to preserve the values of your static and instance variables between transactions.

global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{
}
http://salesforceapexcodecorner.blogspot.in/2011/08/state-management-in-batch-apex-in.html

In Short if you need to send a mail to check number of record pass and failed in batch job counter in that case can you Stateful batch job.
If you want to create one counter and share/ use in each execute method use same.


Using Stateless Batch Apex
Batch Apex is stateless by default. That means for each execution of your execute method, you receive a fresh copy of your object. All fields of the class are initialized, static and instance.

global class SummarizeAccountTotal implements Database.Batchable<sObject>{
}

Please let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
sonali  vermasonali verma
Hi Amit
Thanks for ur prompt reply.
As usual u always give precise answers.

I have one doubt. ​​​How to share batch info across transactions i.e are u saying that batch1 info can be shared with batch2 info.?

what I understand is​ without Database.stateful also I can send emails and have counter info. 
If yes the pls explain clearly how possible?

Thanks
sonali verma​​
 
Amit Chaudhary 8Amit Chaudhary 8
If you want to send the counter or share across the batch job then you need to use Database.stateful batch job.

Using Stateful Batch Apex
If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface. This instructs Force.com to preserve the values of your static and instance variables between transactions.

I hope this will help you.

Thanks
Amit Chaudhary
sonali  vermasonali verma
Hi amit
I read salesforce documentation and it says static variables not preserved between transactions.
​Here's the URL
https://developer.salesforce.com/docs/atlas.en-us.196.0.apexcode.meta/apexcode/apex_batch_interface.htm

Nagesh B 13Nagesh B 13
Hi Amith, i have a requirement that, i have Account Object. Input element is Account . Based on the Account i shoud retrieve Child element case and again based on the case i need to retrieve case child elements and for each account i should get all details and wanna generate xml file. it is batch class? how can i achieve this by using batch class...? any hint please...