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
Sylvie SerpletSylvie Serplet 

How to exclude weekend on recurring task due date?

I have created recurring tasks such as 'Occurs on day 10 of every 1 month'. The problem is when they fall on a week end. I could not figure it out how to make the task falls on the Friday preceding the week end if it falls on a Saturday or a Sunday. All tasks are already entered in the Salesforce. 
Any Idea?
Thank you.
Best Answer chosen by Sylvie Serplet
Sylvie SerpletSylvie Serplet

I have to change the trigger for 'after insert' to 'before insert' to make it works. There is the code and the test class (81% coverage).
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;
}
}

 

All Answers

ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi there,

we dont have any option as such in our standard reccurence functionality for Task to exclude week-ends on selction of option "daily" but we can change them at the time creation using trigger.

you can refer this example and build the logic as per your requirement.

trigger CreateFlowupTask on Account (after insert){
List<Task> tasks = new List<Task>();
List<Account> Acc = Trigger.new;

for (Account Accs : Acc ) {

date shipDate = Accs.ship_date__c;

//add 2 days because your followup task is shipdate plus 2 days
date followUpDate= shipDate.addDays(2);

date checkDate = date.newInstance(1900,1,7);

//count days between 2 days
integer numberDaysInBetween = checkDate.daysBetween(followUpDate);

//modulus calculation in apex
integer sum = math.mod(numberDaysInBetween,7);

date finalFollowUpDate;

//compare and if match found than add days acconding to it
// 0 = sunday , 1 = monday, 2 = tuesday, ......, 6 = saturday
// so if followup day is saturday than add 4 days from your shipdate
// if followup day is sunday than add 3 days from your shipdate


if(sum == 6){ finalFollowUpDate = shipDate.addDays(4); }
else if (sum == 0) { finalFollowUpDate = shipDate.addDays(3); }

else { finalFollowUpDate= shipDate.addDays(2); }


// task creation in trigger

Task tsk = new Task(whatID = Accs.ID, Ownerid = Accs.OwnerId, Subject = '2nd Day follow-up Call', ActivityDate = finalFollowUpDate);
tasks.add(tsk);
}
insert tasks;
}


let me know, if it helps you or need any help :)
shiva.sfdc.backup@gmail.com 
Sylvie SerpletSylvie Serplet
Hi Shiva,
Thank you for your response.
I am using the Task on Case and not on Account and I do not have an extra date such as shipDate to make the calculation. It is the recurring Due date that need to be changed.
I have come accross the following trigger on a forum but it does not look to update the task already entered in Salesforce.
trigger WeekendFinanceTasks on Task (after insert, after update) 
{
 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);
}
 {
 Task tt = new Task 
 (Id = tas.Id );
 update tt; 
 }
 }
 }
 }

Any other thought?
 
Sylvie SerpletSylvie Serplet
In fact we I try to run the trigger in the execute anonymous window I got the following error message:
Line: 1, Column: 1
System.DmlException: Update failed. First exception on row 1 with id 00Tp0000002Az1DEAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WeekendFinanceTasks: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Tp0000002Az1DEAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WeekendFinanceTasks: maximum trigger depth exceeded Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D] Tasktrigger event AfterUpdate for [00Tp0000002Az1D] Task trigger event AfterUpdate for [00Tp0000002Az1D]: [] Trigger.WeekendFinanceTasks: line 22, column 1: []


 
Sylvie SerpletSylvie Serplet

I have to change the trigger for 'after insert' to 'before insert' to make it works. There is the code and the test class (81% coverage).
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;
}
}

 
This was selected as the best answer
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Sylvie Serplet,

I could not get time to look into that. good catch that you found the cause your self :)

shiva.sfdc.backup@gmail.com