Batch code :
global class updateoppforworkflow1 implements Database.Batchable<SObject>, Database.Stateful{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' and Open_Tasks__c=0 and Last_Activity_Age__c >=12 ';
// added below line because Last_Activity_Age__c should be >=12 which we cnt do inside test class
if(test.isRunningTest())
{
Query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' ';
}
system.debug(query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<SObject> scope){
for(Opportunity obj : (Opportunity[]) scope){
if(obj.Stage_change_reminder_count__c != Null){
obj.Stage_change_reminder_count__c = 0;
} else
obj.Stage_change_reminder_count__c = 1;
}
system.debug('list to be updated size :: ' + scope.size());
if(!scope.isEmpty())
{
update scope;
}
}
global void finish(Database.BatchableContext BC){
}
}