Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
:) :) :) :) :)

# 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

:) :) :) :) :)

finally i comeup with that solution.

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

trigger CreateFlowupTask on Account (after insert){

List<Account> Acc = Trigger.new;

for (Account Accs : Acc ) {

date shipDate = Accs.ship_date__c;

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

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

}
}

SRK

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<Account> Acc = Trigger.new;

for (Account Accs : Acc ) {

date shipDate = Accs.ship_date__c;

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