If you would like to send an email notification to the owner of a task when its created.
Using a trigger on the task object before insert you can invoke Messaging.SingleEmailMessage. Below is a code example.
Trigger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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); } } } |
BryantGlums
Hi, Nick
I’ve been visiting your website a few times and decided to give you some positive feedback because I find it very useful. Well done.
Thank you for help and I wish you a great week!