Exam_Date__c = 01/01/2018, Expiration_Date__c = 01/31/2018 (usually 30 days gap)

I want to send an email 7 days prior to expiration, on say eg. 01/25/2018, likewise.

I used the following formula in my apex code

if (Expiration_Date__c -7 <= Today() AND Today() < Expiration_Date__c) {sets a checkbox to true, and that checkbox value is checked by a process which sends an email}

This code is sending emails to all the exams that have been created, I don't understand why. Even the ones that have expiration dates two months ahead.

global class examExpiryAlert implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<Exam__c> objects = [
            SELECT Name, Exam_state__c, Expiration_Date_WF__c, day7Alert__c 
            FROM Exam__c
            WHERE Exam_state__c = 'Active'];

        for(Exam__c e : objects){
            if(e.Expiration_Date_WF__c - 7 <= Date.Today() && e.Expiration_Date_WF__c > Date.Today()) {  
                e.day7Alert__c = True;
        update objects;

1 Answer 1


try with:

        if(e.Expiration_Date_WF__c.addDays(-7) == Date.Today() && e.Expiration_Date_WF__c > Date.Today()) {  
            e.day7Alert__c = True;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.