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
:) :) :) :) :):) :) :) :) :) 

Create Task Using Trigger that automatically exclude weekends

Hi

        I have customer date filed on my account object. And if I save any date it will create 10 follow up task according to that date. Means custom date plus 1 day task, custom date plus 4 day task, custom date plus 10 day task but I want to create task that exclude weekend and holidays or they exclude business hours. So I must have to write trigger that create task. Please help me on that.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
:) :) :) :) :):) :) :) :) :)

finally i comeup with that solution.

-----------------------------------------------------------------------------------------------------------------------------------

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;
}

All Answers

SRKSRK

1st create a custom seeting  in which u mention holidays ant the range of  exclude business hours.


:) :) :) :) :):) :) :) :) :)

But can't we use that formula that display numbers if there is a saturday or sunday and than according to that number  we move forward our task to upcoming weekday. currently I am working on that trigger.

:) :) :) :) :):) :) :) :) :)

finally i comeup with that solution.

-----------------------------------------------------------------------------------------------------------------------------------

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;
}

This was selected as the best answer