0

I wrote this class with a method that sends an email from Sendgrid based on each template id. It works fine except, I have an if else statement which only runs the if and else part and not the else if. The email for the if condition and the else condition are correct. But the emails for the else if condition are getting sent using the template for the else condition. What am I missing here?

public class MailService {

    public static final String CASE_CLOSE_3_DAYS_BEFORE_DEADLINE = 'd-236cee9e85f94e219a3f595ad8224a25';
    public static final String CASE_CLOSE_1_WEEK_AFTER_DEADLINE = 'd-f9736104d9ba410c9ae2748ea53b2def';
    public static final String CASE_CLOSE_30_45_DAYS_AFTER_DEADLINE = 'd-47e918c9be19417eb61699a205b5ed83';
    public static final String CASE_CLOSE_60_DAYS_AFTER_DEADLINE = 'd-ad66132fb5ef4eb9b6cbb624b7ee8edb';
    public static final String CASE_CLOSE_CONSULTANT_EMAIL_DAY67 = 'd-6587bdc25c4542d89095a212e6ee9d8a';


    public static void sendEmailCaseCloseEmployer(Id jobId, String jobTitle, Datetime deadline, String contactName, String email){
        Date threedaysbefore = Date.today().addDays(3);
        Date oneweekafter = Date.today().addDays(-7);
        Date thirtydaysafter = Date.today().addDays(-30);
        Date fortyfivedaysafter = Date.today().addDays(-45);
        Date sixtydaysafter = Date.today().addDays(-60);
        Date deadlinedate = deadline.Date();
        List<String> postEmails = new List<String>();
          if(!String.isEmpty(email)){ 
            postEmails.add(email); 
          }
        Map<String, Object> templateData = new Map<String, Object>{
            'id' => jobId,
            'jobTitle' => jobTitle,
            'contactName' => contactName,
            'submissionDeadline' => deadline.Date()
        };
        if(deadlinedate==threedaysbefore) {
            SendgridService.send(postEmails, CASE_CLOSE_3_DAYS_BEFORE_DEADLINE, templateData);
        } else if(deadlinedate==oneweekafter) {
            SendgridService.send(postEmails, CASE_CLOSE_1_WEEK_AFTER_DEADLINE, templateData);                
        } else if(deadlinedate==thirtydaysafter||deadlinedate==fortyfivedaysafter) {
            SendgridService.send(postEmails, CASE_CLOSE_30_45_DAYS_AFTER_DEADLINE, templateData);                
            } else {
            SendgridService.send(postEmails, CASE_CLOSE_60_DAYS_AFTER_DEADLINE, templateData);      
         }      
    }
}
0

Can you please take a look at below dates

Date threedaysbefore = Date.today().addDays(3);
Date oneweekafter = Date.today().addDays(-7);
Date thirtydaysafter = Date.today().addDays(-30);
Date fortyfivedaysafter = Date.today().addDays(-45);
Date sixtydaysafter = Date.today().addDays(-60);

As from the varliable names it appears you want threedaysbefore date but the expression written will give a date 3 days from today which is 2020-09-08. Similarly for all the other variables. You can also put some debug logs in your code to check what values are returned

System.debug(threedaysbefore );
System.debug(oneweekafter );
4
  • No the calculation is fine. Each of those variables are for the date corresponding to a Deadline date field that I have in my object. The variable names actually represent the email notification that goes out. So threedaysbefore represents the deadline date for which a 3-days-before-deadline email gets sent. My issue here is not what date values are returned. It's the fact that my code is only running the if and else part, and skipping the else if. When the if condition is met, it works, and for all other dates it runs the else condition.
    – mufz
    Sep 5 '20 at 20:19
  • As in your else if condition you are looking for equality so they will only execute when the dealdline exactly matches the oneweekafter, thirtydaysafter or fortyfivedaysafter otherwise it will always go to else part even if the deadline date is in between onweekafter or thirtydaysafter. Hence, the code is working as per the logic designed. Sep 6 '20 at 9:16
  • Yes, that's exactly what I'm saying. I have dates that are supposed to match the else if condition but it still executes the else condition for those results. That's what i'm trying to figure out here.
    – mufz
    Sep 6 '20 at 19:11
  • if your 1st three criteria under if and else if doesn't match then its quite obvious the else condition will execute. if you don't want your else condition to execute then i would suggest removing it or putting it in else if condition for 60 days. Also put debug logs to check what values are coming when deadline is 30 and 40 days Sep 6 '20 at 19:31

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.