You need to sign in to do that
Don't have an account?

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
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
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
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
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
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
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
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
https://www.sfdc-lightning.com/2018/09/batch-class-in-salesforce.html