2

I need to assign accounts to territories. So, as soon as a new account record is created, I want to assign it to a territory based on some business rules and conditions.

I am able to write an trigger and apex class to achieve it but I want to run this code only during the weekdays. So any new accounts coming during weekdays should be assigned immediately why while the ones coming over the weekend should be processed on on Monday.

How can I achieve this? I don't think a trigger can be configured to run during specific times. How can I design for this requirement then?

  • 1
    Can you tell us why you want to delay the ones that come in over the weekends? I can tell you how to disable the trigger over a weekend, but you'd miss processing the records completely if you did. – crmprogdev Mar 2 '17 at 15:47
  • This is a business requirement. Assignments created over weekends will trigger emails to the territory owners and those can get missed if its a weekend. I know it isn't too solid a reasoning but that's the requirement :( – Maneesh Mar 2 '17 at 16:26
3

You could add a check in your trigger as such:

DateTime myDateTime = (DateTime) myDate;
String dayOfWeek = myDateTime.format('E');
// dayOfWeek is Sun, Mon, Tue, etc.
if(dayOfWeek == 'Sun' || dayOfWeek == 'Sat'){
   ..Iterate over records and Set the flag to process later...
   return;
}

To process the records that came in over the weekend I would:

  1. Add a checkbox to the record say "Delayed Processing". Unless you have some other way of figuring out they were not processed
  2. Write a batch to pick those up and set the batch to run on Mondays
  3. Process them and reset the checkbox to false

Note: This may be an X-Y problem. Not so sure you want to delay the process, rather it may be that you want to delay notifications, visibility etc. It would be helpful to know the why you are doing it as there may be a better solution

  • You beat me to it!.I was about to post something like this. – Samir Mar 2 '17 at 15:48
  • 1
    This doesn't get the records that came in over the weekend processed. He still needs a schedulable to run on Monday mornings to pick those records up. – crmprogdev Mar 2 '17 at 15:48
  • @crmprogdev - True I saw "why" and stopped reading. Wording of question did not make it clear. Updated my answer a bit – Eric Mar 2 '17 at 15:50
  • Thanks @Eric ! I almost knew that I'll have to park these weekend records somehow, Your approach will work for me – Maneesh Mar 2 '17 at 16:27
0

You can also do this

Date saturdayDate= Date.newInstance(2016, 12, 31)
If(Math.mod(myDate.daysBetween(saturdayDate),7)<2){ // weekend 
    // Treat records created on the weekend (delayed)
}else{ // weekdays
    // Treat records created on weekdays (immediately)
}
0

You can get the current day in a week using apex as below:

Integer dayOfWeek = Date.today().toStartOfWeek().daysBetween(Date.today())
 if(dayOfWeek>0 and dayOfWeek<6) // here 0 is sunday and 6 is saturday - in united states locale start of week is sunday
 {
  //your code to execute during weekdays!!
 }

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.