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');
        }
      }

closed as off-topic by David Reed, glls, battery.cord, Derek F, Pranay Jaiswal Mar 7 at 16:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – David Reed, glls, battery.cord, Derek F, Pranay Jaiswal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • @DavidReed, Please find the code in question. I just updated it. – KS Kumaar Mar 7 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 Mar 7 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 Mar 7 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. – battery.cord Mar 7 at 14:18
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[] { 'abc2@gmail.com', 'abc2@gmail.com' };
        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

  • One small doubt: Can we get the date from schedule job? – KS Kumaar Mar 7 at 14:27
  • 1
    Just use Date.today(). Edit: I just added a sample code that you could use to solve your problem. – Dahn Mar 7 at 14:32
  • Thank you for the sample code. I will try with the code and surly I'll let you know – KS Kumaar Mar 7 at 14:37
  • If useful, please mark your question as answered. – Dahn Mar 7 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 Mar 8 at 7:17

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