Skip to content

Apex Code for Creating recurring tasks

DESCRIPTION

This article contains the apex code for creating recurring tasks. There are many scenarios where organizations need tasks that need to be completed repeatedly. A few use cases are mentioned below.

  • Create a task for Weekly team meetings
  • Create a task for Monthly customer meetings

SOLUTION

To create recurring tasks in Salesforce, first, you will have to enable the recurring task feature. See how to Enable Recurring Tasks in Lightning Experience

Now you can create recurring tasks using the apex code. See the sample code below for creating a recurring task with each frequency. See the Task Object for more information about each recurring field of the task object.

Weekly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Weekly Recurring Task';
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Priority = 'Normal';
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today();
mTask.RECURRENCEENDDATEONLY = date.today().addYears(1).AddDays(-7);
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursWeekly';
mTask.RECURRENCEINTERVAL = 1;
mTask.RECURRENCEDAYOFWEEKMASK = 8;
insert mTask;

Bi-Weekly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Bi-weekly Recurring Task';
mTask.OwnerId = userInfo.getUserId();
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today();
mTask.RECURRENCEENDDATEONLY = date.today().addYears(2).addDays(-7);
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursWeekly';
mTask.RECURRENCEINTERVAL = 2;
mTask.RECURRENCEDAYOFWEEKMASK = 8;
insert mTask;

Monthly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Monthly Recurring Task';
mTask.OwnerId = userInfo.getUserId();
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Priority = 'Normal';
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today();
mTask.RECURRENCEENDDATEONLY = date.today().addYears(4);
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursMonthlyNth';
mTask.RECURRENCEINTERVAL = 1;
mTask.RECURRENCEDAYOFWEEKMASK = 8;
mTask.RECURRENCEINSTANCE = 'First';
insert mTask;

Quarterly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Quarterly Recurring Task';
mTask.OwnerId = userInfo.getUserId();
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Priority = 'Normal';
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today(); //The date when the recurring task begins. 
mTask.RECURRENCEENDDATEONLY = date.today().addYears(8); //The last date on which the task repeats.
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursMonthlyNth';
mTask.RECURRENCEINTERVAL = 3; //The interval between recurring tasks
mTask.RECURRENCEDAYOFWEEKMASK = 8;
mTask.RECURRENCEINSTANCE = 'First';
insert mTask;

Yearly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Yearly Recurring Task';
mTask.OwnerId = userInfo.getUserId();
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Priority = 'Normal';
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today();
mTask.RECURRENCEENDDATEONLY = date.today().addYears(8);
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursYearlyNth';
mTask.RECURRENCEDAYOFWEEKMASK = 8;
mTask.RECURRENCEINSTANCE = 'First';
mTask.RECURRENCEMONTHOFYEAR = 'January';
insert mTask;

Bi-Yearly Recurring Task

Task mTask = new Task();
mTask.Subject = 'Bi-Yearly Recurring Task';
mTask.OwnerId = userInfo.getUserId();
mTask.WhatId = recordId; //Assign Related To record Id
mTask.Priority = 'Normal';
mTask.Status = 'Open';
mTask.ISRECURRENCE = true;
mTask.RECURRENCESTARTDATEONLY = date.today();
mTask.RECURRENCEENDDATEONLY = date.today().addYears(10);
mTask.RECURRENCETIMEZONESIDKEY = 'America/New_York';
mTask.RECURRENCETYPE = 'RecursMonthlyNth';
mTask.RECURRENCEINTERVAL = 6;
mTask.RECURRENCEDAYOFWEEKMASK = 8;
mTask.RECURRENCEINSTANCE = 'First';
insert mTask;

Note: There’s a limit to how many recurring tasks you can create in a series, and the limit varies based on task frequency. The latest possible end date depends on the maximum number of tasks. For example, a recurring task that repeats every month supports up to 60 tasks in the series. In this case, the end date can be scheduled up to five years after the start date. For more details see the Considerations of using Salesforce Tasks.

FREQUENCYMAXIMUM NUMBER OF TASKSLATEST END DATE (FROM THE START DATE)
Daily100100 days
Weekday10020 weeks
Weekly531 year
Monthly605 years
Yearly1010 years