No More Weekend Tasks

If you’re like us, you probably have a lot of Workflow Rules and Tasks setup in Salesforce. It’s a great feature that helps to automate processes and it ensures accurate and timely notification across your organization.

One thing that has always bothered me about Salesforce’s Workflow is that it doesn’t take weekends into consideration (unlike Case Escalation Rules). Well if you have access to Apex Code, here is a simple Task Trigger that will update Tasks created with a due date on Saturday or Sunday to instead have a due date on Monday or Tuesday.

Just create a new Task Trigger with the following code:

trigger WeekendTasks on Task (before insert) {
	for (Task tas : Trigger.new) {
			Date origin = Date.newInstance(1900,1,6);
			Date due = tas.ActivityDate;
			Boolean reminder = tas.IsReminderSet;
			Integer x = origin.daysBetween(due);
			Integer day = Math.mod(x,7);
			if (day < 2 ) {
				tas.ActivityDate = (due + 2);
				if (reminder == true ) {
					Datetime rem = tas.ReminderDateTime;
					tas.ReminderDateTime = (rem + 2);
				}
			}
	}
}

Special thanks to Eric Bezar at The Official Salesforce Blog for help with the original formula.

  • Share/Bookmark

Related posts:

  1. Tracking the duration of a call
  2. Creating a Formula Field to Sort Records in True Alphabetical Order
  3. Deriving Website from Email Addresses
  4. Automating Support Requests
  5. Pick a number 1-10, randomly, with Apex

One Comment to “No More Weekend Tasks”

  1. Sai LavuNo Gravatar Says:

    I would suggest not adding two days to the reminder date always as it can be different to Activity date. Thanks for sharing it.

    trigger WeekendTasks on Task (before insert)
    {
    for (Task task : Trigger.new)
    {
    Date origin = Date.newInstance(1900,1,6);
    Integer daysBetween = origin.daysBetween(task.ActivityDate);
    Integer daysMod = Math.mod(daysBetween, 7);
    if (daysMod < 2)
    task.ActivityDate = (task.ActivityDate + 2);

    if (task.IsReminderSet)
    {
    Date reminderDate = Utility.getDate(task.ReminderDateTime);
    daysBetween = origin.daysBetween(reminderDate);
    daysMod = Math.mod(daysBetween, 7);
    if (daysMod < 2)
    task.ReminderDateTime = (task.ReminderDateTime + 2);
    }
    }
    }

    public class Utility
    {
    public static date getDate(Datetime dt)
    {
    Integer day = dt.Day();
    Integer month = dt.Month();
    Integer year = dt.Year();
    return date.newInstance(year,month,day);
    }
    }

Post a Comment