To maintain variable value inside the Batch class, Database.Stateful is used.
Sample Class:
Sample Class:
global class Class_Name implements Database.Batchable<sobject>, Database.Stateful{
Integer i = 0;
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocatory(‘SELECT Id, Name, Sequence_Number__c FROM Employee__c’);
}
global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee){
for(Employee__c e : listEmp){
e.Sequence_Number__c = i;
i += 1;
}
}
global void finish(Database.BatchableContext bc){
}
}
here i value will be maintained even though execute method is called several times.
Cheers!!!
is it mandatory to use global with database.stateful ?
Yes it is, If u want to maintain the state accross