function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yam AdminYam Admin 

Apex Trigger to update Task Due Date when falling on a weekend

We have several Workflow Rules that create Workflow Tasks x days later than the Rule trigger date.  We would like to create an Apex Trigger that will automatically calculate the day of the week the new Task is due on and if on a weekend, change the date to 2 day later.

I know it's possible to calculate Day of Week in a formula field thanks to this blog post.

I've already read the Apex Language Reference and created some simple Apex Triggers, but I just don't have enough experience working with Apex Triggers to know how to do this.  Any help or samples would be greatly appreciated.


We built a similar trigger for one of our clients, try this out:


trigger WeekendTasks on Task (after insert) {
for (Task tas : {
if (tas.Subject == 'Matt Test') {
Date origin = Date.newInstance(1900,1,6);
Date due = tas.ActivityDate;
Integer x = origin.daysBetween(due);
Integer day = Math.mod(x,7);
if (day < 2 ) {
Task tt = new Task (
Id = tas.Id,
ActivityDate = (tas.ActivityDate + 2),
Description = String.valueOf(day)
update tt;

Hay MKPartners.It is not working for Saturday.