Skip to main content
Tweeted twitter.com/StackSalesforce/status/991805333621366785
Clean up code formatting
Source Link
David Reed
  • 93.7k
  • 14
  • 90
  • 166
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;
    
     }
    }
}

}

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;
        }
       
    
     } else {
       
        Visit_Events__c event = new Visit_Events__c(
            Name = 'Campus Visit Experience',
            Date__c = eventDate
        );
        insert event;
        
     }
    
}

}

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;
            }
        } else {
            Visit_Events__c event = new Visit_Events__c(
                Name = 'Campus Visit Experience',
                Date__c = eventDate
            );
            insert event;
        }
    }
}
Source Link

Scheduled Apex not using current data

I have a scheduled Apex class that creates one new record every night with a date field that needs to equal today + three months. I've tried using Date.today().addMonths(3), translating System.now() to a Date format, and trying to grab the date of the most recent record and adding one day to it.

However, the date it references is always the start date of the job, not the current date. Similarly, lastEvent returns the most recent record as of the job start date, not the actual most recent record. This results in every new record created by the scheduled job to have the same date rather than incrementing by one day.

Is this how scheduled jobs are supposed to function? How do I create one record every night with the correct date of today + three months?

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;
        }
       
    
    } else {
       
        Visit_Events__c event = new Visit_Events__c(
            Name = 'Campus Visit Experience',
            Date__c = eventDate
        );
        insert event;
        
    }
    
}

}