1

I want to run a schedule class every 1st date of the month but if it is a saturday or a sunday I want to execute it on the coming monday.I have created a CRON expression that helps it to execute on 1st every month but I want to know how to skip weekends.

global class MonthlyNotificationtoProfileUsers implements schedulable
{
     public static String CRON_EXP = '0 0 0 1 * ? *';

    global static String scheduleIt() {
        MonthlyNotificationtoProfileUsers sm = new MonthlyNotificationtoProfileUsers();
        return System.schedule('Monthly Reconciliation', CRON_EXP, sm);
    }

  global void execute(SchedulableContext sc)
   {
      EmailTemplate et = [SELECT id FROM EmailTemplate WHERE Id = '00X55000000E6vb'];
      List<Profile> userProfile = [select id from profile where Name='CC - User Access'];
      OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'lcsalesforceadmin@mcmcg.com'];
      List<User> userList = new List<User>();
      userList = [Select id,IsActive from user where ProfileID IN:userProfile AND IsActive = TRUE];
      system.debug('userList-->'+userList);
      system.debug('userListsize-->'+userList.size());

      List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage> ();

      if(userList.size() >0 && userList != Null)
      {
        for(User user : userList)
          {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setTargetObjectId(userList[0].Id);
                    mail.setSaveAsActivity(false);
                    mail.setTemplateId(et.id);
                    mail.setSaveAsActivity(false);
                    mails.add(mail);
          }
                    Messaging.sendEmail(mails);
      }
   }
}
1

You can let job run on 1st every month and if it is satureday or Sunday you can reschedule to next Monday.

global class MonthlyNotificationtoProfileUsers implements schedulable
{
     public static String CRON_EXP = '0 0 0 1 * ? *';

    global static String scheduleIt() {
        MonthlyNotificationtoProfileUsers sm = new MonthlyNotificationtoProfileUsers();
        return System.schedule('Monthly Reconciliation', CRON_EXP, sm);
    }

  global void execute(SchedulableContext sc)
   {
      Date d = System.today();
      Datetime dt = (DateTime)d;
      String dayOfWeek = dt.format('EEEE');
      if(dayOfWeek!='Saturday' && dayOfWeek!='Sunday'){
          EmailTemplate et = [SELECT id FROM EmailTemplate WHERE Id = '00X55000000E6vb'];
          List<Profile> userProfile = [select id from profile where Name='CC - User Access'];
          OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'lcsalesforceadmin@mcmcg.com'];
          List<User> userList = new List<User>();
          userList = [Select id,IsActive from user where ProfileID IN:userProfile AND IsActive = TRUE];
          system.debug('userList-->'+userList);
          system.debug('userListsize-->'+userList.size());

          List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage> ();

          if(userList.size() >0 && userList != Null)
          {
            for(User user : userList)
              {
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        mail.setTargetObjectId(userList[0].Id);
                        mail.setSaveAsActivity(false);
                        mail.setTemplateId(et.id);
                        mail.setSaveAsActivity(false);
                        mails.add(mail);
              }
                        Messaging.sendEmail(mails);
          }
       }
    else{
        String CRON_EXP_NEXT_MONDAY='0 0 0 ';
        if(dayOfWeek=='Saturday'){
            CRON_EXP_NEXT_MONDAY=CRON_EXP_NEXT_MONDAY+'3';
            }
        else if(dayOfWeek=='Sunday'){
            CRON_EXP_NEXT_MONDAY=CRON_EXP_NEXT_MONDAY+'2';
            }
        CRON_EXP_NEXT_MONDAY=CRON_EXP_NEXT_MONDAY+' '+d.month() +' ?';
        MonthlyNotificationtoProfileUsers sm = new MonthlyNotificationtoProfileUsers();
        System.schedule('Monthly Reconciliation', CRON_EXP_NEXT_MONDAY, sm);
        }
   }
}
| improve this answer | |

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.