+ Start a Discussion
KaranrajKaranraj 

Business Hours

Hi everyone,

 

I just written apex class which implements schedulable interface, i want to run that apex class only on my bussiness hours

How to schedule that class?

 

Thanks,

Karan

kiranmutturukiranmutturu

you can query the bussinesshours object  and get the required values to schedule the job.... 

KaranrajKaranraj

Will you explain me with example?.I have no idea, it still blank to me, Help me to sove this issue..

Soul AgesSoul Ages

The Schedulable interface runs similar to a cron in a Unix environment. You can make it run every x time (daily, monthly, weekly, first monday of month, etc.). For your problem, I would suggest setting the frequency of the job (want to run every hour, for example). Then in the object you check if it's the proper hours to run.

In http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm you can find information on how the Scheduler works.

KaranrajKaranraj

Hi Soul Ages,

 

     Thanks for your suggestion.Now only issue am facing is, my class should not run on public holidays, i.e., we can able set company business hours and hloidays in the company profile, suppose today is holiday in my company profile my apex class should not run today. How to schedule my apex class in that way?.Is there any possibilities to do that?

 

Soul AgesSoul Ages

You do it the same way as you do for BusinessHours, by checking when to run in the code. The class would look something like:

global class RunInBusinessHours implements Schedulable{
   global void execute(SchedulableContext sc) {
       if (isWorkDay() && isBusinessHours()) {
          doActualWork();
       }
   }
}

where the isWorkDay and isBusinessHours are the methods you define to check for when to run and the doActualWork contains the code you want to run.

Then you can schedule the class to run every hour, so that each our it checks if it should do the work and do it only when it needs to.