Skip to content

Instantly share code, notes, and snippets.

@forcethesales
Last active February 14, 2020 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save forcethesales/0e2e39a1ed74119eac5dbcdc23f8de99 to your computer and use it in GitHub Desktop.
Save forcethesales/0e2e39a1ed74119eac5dbcdc23f8de99 to your computer and use it in GitHub Desktop.
Trigger a Follow-Up Task When an Email Task is Inserted
// Trigger Handler to create a follow-up task when an email is send through Salesforce's "send an email button"
// based off of Kieren Jameson's Cooking with Code: A Tasty Trigger Treat
// http://womencodeheroes.com/2015/07/cooking-with-code-a-tasty-trigger-treat-apex-triggers/
public class TaskTriggerHandler {
// declare a method, which accepts a list of tasks (EmailList) as a parameter.
public static void createNewTask (List<Task> EmailList) {
// Set up Due Date variable for the new tasks, which is 1 month from today
Date dDate = Date.today(); // set's today's date
dDate = dDate.addMonths(1); //adds one month
// TODO: set # of months in a custom setting?
// Create list to keep the New Tasks you want to create in the database
List<Task> newTasks = new List<Task>();
// Loop through all the emails (tasks) that were just added by the trigger
for (Task t : EmailList) {
// filter criteria for when the trigger should fire
// if the subject starts with "Email:" && it is attached to a contact
// TODO: Could you move the string to filter by into a custom setting?
if(t.subject.startsWith ('Email:') && t.WhoId != null) {
// create --> new task called ToDoItem.
Task toDoItem = new Task (
// details of the new tasks being created
Subject = 'Follow Up on: ' + t.subject,
status = 'Not Started',
ActivityDate = dDate,
whoId = t.whoId,
Priority = 'High'
);
newTasks.add(toDoItem); // populate list of new tasks with each task
}
}
Insert newTasks; // insert into the Salesforce DB outside of the "for Loop".
}
}