User Story
As a sales representative, I want a trigger to automatically create and assign a Task to the owner of a Case when a new Case is created, so that the owner can follow up on the Case in a timely manner. The Task should have a due date of one day from the Case creation date, and the subject should include the Case number and summary. This will help ensure that all Cases are properly addressed and nothing falls through the cracks.
Code
trigger CaseTrigger on Case (after insert, after update) {
List<Task> newTasks = new List<Task>();
for (Case c : Trigger.new) {
if (c.OwnerId != null) {
Task t = new Task();
t.WhatId = c.Id;
t.Subject = 'Follow up on case ' + c.CaseNumber;
t.ActivityDate = Date.today()+1;
t.OwnerId = c.OwnerId;
newTasks.add(t);
}
}
insert newTasks;
}