0

I would like to send out an email to the customer automatically every month on the below dates. The day to send out the email will depend on whether it’s a 4- or 5-week month.

4 weeks in a month 17th 1st email and 24th 2nd email

5 weeks in a month 24th 1st email and 31st 2nd email

So, which means

  1. If current month contains 4 weeks(Sunday) and If its contains 28 or 29 (February case)/30/31 days need to send on 17th and 24th

  2. If current month contains 5 weeks(Sunday) and If its contains 31 days need to send on 24th and 31st, other wise 17th and 24th need to send email.

Can some one implement the logic and please help me in this.

Below is the my attempt which I tried:

      Integer mnth = Date.Today().month();
      Integer date_value = Date.Today().day();

      System.debug('*Date*******'+date_value);
      System.debug('*Month*******'+mnth);// 1=jan

      if(mnth == 2 || mnth == 4 || mnth == 6 || mnth == 9 || mnth == 11){

        if(date_value==17){
          System.debug('Send 17th date email');
        }
        if(date_value==24){
          System.debug('Send 24th date email');
        }
      }

      if(mnth == 1 || mnth == 3 || mnth == 5 || mnth == 7 || mnth == 8 || mnth == 10 || mnth == 12){

        if(date_value==24){
          System.debug('Send 24th date email');
        }
        if(date_value==31){
          System.debug('Send 31st date email');
        }
      }
4
  • @DavidReed, Please find the code in question. I just updated it.
    – KS Kumaar
    Commented Mar 7, 2019 at 14:02
  • 2
    Yes, I removed my comment for that reason. I still don't see a statement of what specific issue you need help with, though. The community will be much more able to help you if you include a concise description of how this code does not work for you and what you need help fixing.
    – David Reed
    Commented Mar 7, 2019 at 14:05
  • @DavidReed, Thank you for the info. I need to find how many weeks current month contains based on Sunday(Explained in question) in the same way how many days thus same month contains? So here I Need to help find the answer
    – KS Kumaar
    Commented Mar 7, 2019 at 14:09
  • 2
    @KSKumaar Davids trying to figure out whats "wrong" with the code you've provided. Theres got to be an issue, otherwise you wouldn't be here. You should make an edit to describe what you tried and why it doesn't work. Commented Mar 7, 2019 at 14:18

1 Answer 1

1

Just use the method in Date class to retrieve the number of days in a month (it takes into consideration if it is a Leap Year):

daysInMonth(year, month)

To know how many weeks, just calculate daysInMonth / 7.

I coded a helper class for your case wrapping those functionalities:

public class DateUtils {
    public static integer getWeekCount(Date d) {
        Integer days = Date.daysInMonth(d.year(), d.month());
        return days/7;
    }

    public static integer getDayCount(Date d) {
        Integer days = Date.daysInMonth(d.year(), d.month());
        return days;
    }
}

Solution for your case:

        Date today = Date.today();
        Integer weeks = DateUtils.getWeekCount(today);
        Integer days = DateUtils.getDayCount(today);
        String firstEmailSubject = '';
        String secondEmailSubject = '';
        String firstEmailBody = '';
        String secondEmailBody = '';
        Boolean send = false;

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = new String[] { '[email protected]', '[email protected]' };
        message.optOutPolicy = 'FILTER';        

        // 4 weeks and 28 to 31 days...
        if(weeks == 4 && 28 <= days && days <= 31) {
            if(today.day() == 17) {
                // Send first email
                message.subject = firstEmailSubject;
                message.plainTextBody = firstEmailBody;
                send = true;
            } else if (today.day() == 24) {
                // Send second email
                message.subject = secondEmailSubject;
                message.plainTextBody = secondEmailBody;
                send = true;
            }

        // 5 weeks and 31 days...
        } if(weeks == 5 && days == 31) {
            if(today.day() == 24) {
                // Send first email
                message.subject = firstEmailSubject;
                message.plainTextBody = firstEmailBody;
                send = true;
            } else if (today.day() == 31) {
                // Send second email
                message.subject = secondEmailSubject;
                message.plainTextBody = secondEmailBody;
                send = true;
            }

        // 5 weeks but not 31 days...
        } else if(weeks == 5 && days != 31) {
            if(today.day() == 17) {
                // Send first email
                message.subject = firstEmailSubject;
                message.plainTextBody = firstEmailBody;
                send = true;
            } else if (today.day() == 24) {
                // Send second email
                message.subject = secondEmailSubject;
                message.plainTextBody = secondEmailBody;
                send = true;
            }
        }

        // Send if necessary.
        if(send) {
            Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);    
        }

Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

5
  • One small doubt: Can we get the date from schedule job?
    – KS Kumaar
    Commented Mar 7, 2019 at 14:27
  • 1
    Just use Date.today(). Edit: I just added a sample code that you could use to solve your problem.
    – Dahn
    Commented Mar 7, 2019 at 14:32
  • Thank you for the sample code. I will try with the code and surly I'll let you know
    – KS Kumaar
    Commented Mar 7, 2019 at 14:37
  • If useful, please mark your question as answered.
    – Dahn
    Commented Mar 7, 2019 at 15:06
  • Yes @Dahn. Thank you. I changed the code a bit according to requirement and the issue has been solved now.
    – KS Kumaar
    Commented Mar 8, 2019 at 7:17

Not the answer you're looking for? Browse other questions tagged .