Controller:
global class QuarterlyRollupBatch implements Database.batchable<Opportunity> {
Public Date lastDateOfThisQuarter,fiscalYearStartDate,firstDateOfNextQuarter;
Public Integer quarter;
global Iterable<Opportunity> start(Database.BatchableContext info) {
Integer FiscalYearStartMonth = 1;// [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
Date todaydate = system.today();
fiscalYearStartDate = date.newinstance(system.today().year(), FiscalYearStartMonth, 1);
quarter = ((system.today().month() - FiscalYearStartMonth) / 3) + 1;
Integer addMonths = quarter * 3;
lastDateOfThisQuarter = fiscalYearStartDate;
//Date closeDate = [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;
// this is the last date of the current quarter
lastDateOfThisQuarter = lastDateOfThisQuarter.addMonths(addMonths).addDays(-1);
firstDateOfNextQuarter=lastDateOfThisQuarter.addDays(1);
System.debug('@@@ '+firstDateOfNextQuarter);
List<Opportunity> oppList=[select id,name,Est_Syndication_Completion_Date__c,income_Timing__c,Owner.Name from Opportunity where Est_Syndication_Completion_Date__c <: firstDateOfNextQuarter and Est_Syndication_Completion_Date__c >=: todaydate and income_Timing__c='Future Opportunity'];
system.debug('Opplist'+oppList);
return oppList;
}
global void execute(Database.BatchableContext info, List<Opportunity> oppList) {
if (oppList.size() > 0) {
for( Opportunity o:oppList) {
o.income_Timing__c = 'Current Opportunity';
}
update oppList;
}
}
global void finish(Database.BatchableContext info){ }
}
global class QuarterlyRollupBatchsch implements Schedulable {
global void execute(SchedulableContext sc) {
Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;
Integer Currentmonth = Date.Today().Month();
QuarterlyRollupBatch createquarterupdate= new QuarterlyRollupBatch();
if(currentmonth==FiscalYearStartMonth || math.mod(Currentmonth,3)==1) {
database.executebatch(createquarterupdate,100);
}
}
}