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
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12 

How to exclude Weekends on Workflow Task Due Date?

Hello Everyone,

How to exclude Weekends on Workflow Task Due Dates?
On Workflow Tasks, we are setting up a due date extending with 'X' days, here, we need to exclude the weekends.

For Example, if we setup a workflow task to "Rule Triggered Date + 5 Days" and if that 5th day falls on weekend either Saturday or Sunday, SF should automatically consider the next Business Day, i.e Monday.

Different Workflows have different Due Dates, need to work it for all Workflow Tasks (on different Objects), irrespective of Due Date.
And Different Workflows have multiple actions like Email Alert, Field Update and Outbound Messages along with Tasks.

Can any one please help me out on achieving same?

Thanks in Advance,
Ranjith.
NagendraNagendra (Salesforce Developers) 
Hi Ranjith,

We don't have any option as such in our standard recurrence functionality for Task to exclude weekends on the selection of option "daily" but we can change them at the time creation using a trigger.

Please find the sample code below and tweak it as per your requirement.
trigger WeekendTasks on Task (before insert) {
 
 for ( Task tas : Trigger.new )
    {
        if ( tas.Type == 'Finance - Recurring'&& tas.ActivityDate != null )
        {
            Date origin = Date.newInstance(1900,1,6);
            Date due = tas.ActivityDate;
            Integer x = origin.daysBetween(due);
            Integer day = Math.mod(x,7);
            
if ( day == 0 ) // Saturday
{
    tas.ActivityDate = (tas.ActivityDate -1);
}
else if ( day == 1 ) // Sunday
{
    tas.ActivityDate = (tas.ActivityDate -2);
}
}
}
}


@isTest
private class TestWeekendTasks {

    static testMethod void testWeekendTasks() {

Task t = new Task();
t.ActivityDate = Date.today();
t.Type = 'Finance - Recurring';
insert t;
}
}
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
 
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi Nagendra,

Thanks for your reply,

The above code will work for all Tasks perfectly, but, I want to exclude weekends for Workflow (auto) Tasks only, we can give the users to put their own Tasks to Weekends manually, if they want.

How do we achieve this?

Thanks,
Ranjith.
sfdcdudesfdcdude
Hi Nagendra,

In your above logic, you have used date datype. could you please provide your answer if we are having datetime datatype.

for example, I am having a datetime field called (Submit_deal_datetime__c). When user selects a date (Lets say, weekend date (06/12/2021) or (06/13/2021) which falls under saturday and sunday respectively), then in another field called (submit deal_report__c) should populate firday'date( 06/11/2021). Please let me know if you are clear. Else will connect and solve this. Thanks.