 ShowAll Questionssorted byDate Posted :) :) :) :) :)

# Create Task Using Trigger that automatically exclude weekends

Hi

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

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