global class cc_scheduledVisitEvent implements Schedulable {
public static String CRON_EXP = '0 0 3 * * ?';
public Date lastEvent = [SELECT Date__c FROM Visit_Events__c WHERE Date__c != null ORDER BY Date__c DESC LIMIT 1].Date__c;
public Date eventDate = lastEvent.addDays(1);
public Date Sunday = eventDate.toStartOfWeek();
public integer eventDoW = Sunday.daysBetween(eventDate);
global static String scheduleIt() {
cc_scheduledVisitEvent ve = new cc_scheduledVisitEvent();
return System.schedule('Create Visit Event', CRON_EXP, ve);
}
global void execute(SchedulableContext ctx) {
if (eventDate.month() == 5 || eventDate.month() == 6 || eventDate.month() == 7 || eventDate.month() == 8){
if (eventDoW != 0 || eventDoW != 6){
Visit_Events__c event = new Visit_Events__c(
Name = 'Experience Campus Summer',
Date__c = eventDate,
);
insert event;
insert event;
}
}
} else {
Visit_Events__c event = new Visit_Events__c(
Name = 'Campus Visit Experience',
Date__c = eventDate
);
insert event;
insert event;
}
}
}
}