trigger Trigger_Task_Send_Email on Task (before insert) {
// We are using after insert so that we can notify after creation
for(Task tsk: Trigger.New)
{
// Get the OwnerID for this task
List<User> owners = [select Name,email from User where id = :tsk.OwnerId LIMIT 1];
for(User owner : owners)
{
// For each owner
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List sendTo = new List();
sendTo.add(owner.email);
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('nick@huber.codes');
mail.setSenderDisplayName('Nick Huber');
// (Optional) Set list of people who should be CC'ed
List ccTo = new List();
ccTo.add('nick@huber.codes');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject('NEW TASK ASSIGNED TO YOU');
String body = 'Dear ' + owner.Name + 'You have a new task assigned to you';
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
Messaging.sendEmail(mails);
}
}
}