function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajevlsRajevls 

Custom Email for tasks

Can we have a custom email for tasks through triggers. 

 

Any code snipet will be very helpful

LuciferLucifer

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
42
43
44
45
46
47
48
49
50
51
52
53
54
trigger Trigger_Task_Send_Email on Task (after insert) {
    string objPrefix = Schema.SObjectType.Annual_Enrollment_Request__c.getKeyPrefix();
    Set<Id> ownerIds = new Set<Id>();
   
    for(Task tsk: Trigger.New)
        ownerIds.add(tsk.ownerId);
   
    // Build a map of all users who are assigned the tasks.
    Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]);
    for(Task tsk : Trigger.New)
    {
    if(tsk.IsAnnualEnrollment__c==true){
    system.debug('Task id is '+tsk.Id);
        User theUser = userMap.get(tsk.ownerId);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {theUser.Email};
        mail.setToAddresses(toAddresses);    // Set the TO addresses
        mail.setSubject('A task has been created for you');    // Set the subject

        // Next, create a string template. Specify {0}, {1} etc. in place of actual values.
        // You can replace these values with a call to String.Format.
        String template = 'Hello {0}, \nYour task has been created. Here are the details - \n\n';
        template+= 'Subject - {1}\n';
        template+= 'Due Date - {2}\n';
        template+= 'Status - {3}\n';
        //template+= 'My Test Field - {3}\n';
        String duedate = '';
        if (tsk.ActivityDate==null)
            duedate = '';
        else
            duedate = tsk.ActivityDate.format();
       
      //  List<String> args = new List<String>();
        //args.add(theUser.Name);
     //   args.add(tsk.Subject);
       // args.add(duedate);
        //args.add(tsk.status);
        //args.add('<a href="https://cs3.salesforce.com/'+tsk.Id+'">Click here to open task<a/>');
       // args.add(tsk.MyTestField__c);
       
        // Here's the String.format() call.
       // String formattedHtml = String.format(template, args);
       
       // mail.setPlainTextBody(formattedHtml);
        mail.setHTMLBody('Hello  ' +theUser.name+' '+ 'a subject '+tsk.Subject+' '+'with Priority '+tsk.status +' '+ 'and duedate ' + tsk.Activitydate +' '+'is been created.'+'<a href="https://cs3.salesforce.com/'+tsk.Id+'">Please click here to open task<a/>');
        if (tsk.whatId != null && ((string)tsk.whatId).startsWith(objPrefix)) 
        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        
      }  // End of if loop which is verifying for Annual Enrollment object
        
    } // End of for Loop


}