Last active
February 14, 2020 11:04
-
-
Save forcethesales/0e2e39a1ed74119eac5dbcdc23f8de99 to your computer and use it in GitHub Desktop.
Trigger a Follow-Up Task When an Email Task is Inserted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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". | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apex Test Class https://gist.github.com/forcethesales/a92526f043ac65d1dfe09f985d2c5360
Trigger Handler https://gist.github.com/forcethesales/0e2e39a1ed74119eac5dbcdc23f8de99
Task Trigger https://gist.github.com/forcethesales/337b0fbeceefffa89176592effbbdd47