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
SriniSrini 

Task Email Notification

Hi All,

Email notification when delegated task is complete.:
For the above requirement i have written a one trigger on Task object.That trigger working as expected.But here my problem is template structure as of now we are using standard template but when assigned the task they will get like this mail.

Task Email content does not reference to actual task
 
I got below email, which looks to be not correct
 
-----Original Message-----
From: ABC (ABC Connections)
Sent: Friday, April 29, 2016 4:42 PM
To: XYZ (ABC Connections)
Subject: A task has been updated
 
Hello ABC,
Your task has been Completed. Here are the details -
 
Subject -Test of Auto Email
Status - Completed
Priority - Normal
 
This should have been followed standard template like below 
 
To: ABC
 
XYZ has updated the following new task:
 
Subject: Test of Auto Email
Opportunity: test11
Status - Completed 
Priority: Normal
 
For more details, click the following link:
 
https://my.salesforce.com/00T22000001nTBP

Can please any one help above template

Thanks in Advance

 
Santosh Kumar 275Santosh Kumar 275

Instead of using standard template. Send email from trigger itself. There you set all the details as per your requirement and need.

Please refer this link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm
for sending Outbound Email.

Or either this will help you out.
 

trigger Trigger_Task_Send_Email on Task (before update) {

    Set<Id> ownerIds = new Set<Id>();   
    for(Task tsk: Trigger.New)
        ownerIds.add(tsk.ownerId);
   
    Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]);
    for(Task tsk : Trigger.New)
    {
        User taskOwner = userMap.get(tsk.ownerId);
        String[] toAddresses = new String[] {taskOwner.Email};
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);    
        mail.setSubject('A task owned by you has been updated');    
        
        String template = '{0}, has updated the following new task:- \n\n';
        template+= 'Subject - {1}\n';
        template+= 'Status - {2}\n';
        template+= 'Priority - {3}\n';
        
        List<String> args = new List<String>();
        args.add(tsk.LastModifiedBy.name);
        args.add(tsk.Subject);
        args.add(tsk.Status);
        args.add(tsk.Priority);

        String formattedHtml = String.format(template, args);
       
        mail.setPlainTextBody(formattedHtml);
        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

Hope this will help you out.
SriniSrini
Hi Santhosh,

Thanks for your help...But I want to know the after completed task status.it should be showing task details in the mail along with the particular Sandbox URL Like...

Your task has been Completed. Here are the details -
 
Subject -Test of Auto Email
Status - Completed
Priority - Normal

https://my.salesforce.com/00T22000001nTBP

But as of now in my code the above Http Url is not showing.... Clould you please help me that

Thanks again
Santosh Kumar 275Santosh Kumar 275
Hi srini
You can add one more line to add the link in template. (@line no 21 of previous code)Like : 
template+= '\n\nFor more details, click the following link: \n\n';
template+= URL.getSalesforceBaseUrl().toExternalForm() + '/' + tsk.Id;
Hope this will help you.

Mark this answer as best answer if this solves your need.
SriniSrini
Hi Santhosh,

The above code working as expected.But i need one more help, here we need to restrict the email notifications to only those tasks created by the specific roles/profiles.How we need to achieve them. Can you please help me on this.

Here the Trigger Code the one which i have using currently :

trigger TaskEmailNotification  on Task (before update ) {
    Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New){
        if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById){
            ownerIds.add(tsk.CreatedById);      
        }        
    }    
    // 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]);
    List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();    
    for(Task tsk : Trigger.New) {
        if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById) {
            User theUser = userMap.get(tsk.CreatedById);
            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 updated');   // Set the subject 
            // Next, create a string template. Specify {0}, {1} etc. in place of actual values.
            String template = 'Hello {0}, \n\n Your Task has been Completed. Here are the details - \n\n';
            //  template+= 'taskid -'+tsk.id+' \n';
            template+= 'Subject: '+tsk.Subject+' \n';
            template+= 'Status: '+tsk.Status +' \n';
            template+= 'Priority: '+tsk.Priority +' \n';
            template+= '\n\nFor more details, click the following link: \n\n';
            template+= URL.getSalesforceBaseUrl().toExternalForm() + '/' + tsk.Id;               
            List<String> args = new List<String>();     
            args.add(theUser.Name);
            // Here's the String.format()
            String formattedHtml = String.format(template, args);
            mail.setPlainTextBody(formattedHtml);
            emailMsglist.add(mail);    
        }     
    }
    Messaging.SendEmail(emailMsglist);
}

Thanks
Santosh Kumar 275Santosh Kumar 275

So check the condition before sending email i.e

If(UserInfo.getProfileId() == ProfileId){
/** your code **/
}

ProfileId : is the profile throughwhich you want to send the email.

Hope this will help you. Let me know if younedd any othe help. 
Santosh Kumar 275Santosh Kumar 275

So check the condition before sending email i.e

If(UserInfo.getProfileId() == ProfileId){
/** your code **/
}

ProfileId : is the profile throughwhich you want to send the email.

Hope this will help you. Let me know if younedd any othe help. 
SriniSrini
Hi Santhosh,

We are facing some problem in the above trigger ..as of now the above code is working as expected the problem is here in task object we have some record types are there, in that if we select standard record type the above code is working,if we select some RMA updates,Case record types that time task owner getting the mail like your task has been Completed but hear we want emails are sending to the task who has created instead of sending it to assignee. .can you please help me on this


Thanks 
Santosh Kumar 275Santosh Kumar 275
You want to send the mail to assignee or owner please confirm?

In this case you are sending the email to owner.
Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New){
        if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById){
            ownerIds.add(tsk.CreatedById);      
        }        
    }

Here in your code you have created the set of ownerIds.
SriniSrini
Hi @Santhosh,
 
The same code we are using the one which you have mentioned above.But my problem is we have some recordtypes in task Object.Like "UPDATE","WEBUPDATE" e.t.c.like that.Hear "Standard" is the default recordtype .If we select default recordtype then trigger working as expected so that time Task assignee getting mail like your "Giri has assigned you the following new task:"  Hear Giri is the task Owner,  Shyam is the Assignee, after recieving mail shyam.Shyam wants to be changed status inprogress to Completd.after complete the task task owner(Giri) getting mail like this "Your task has been completed". as of now that senario working.

Hear the main problem is in the record type section if selecet any of the other recordtype apart from Standard after assign the task to assignee Shyam getting mails like " Giri has assigned you the following new task" .that time shyam hasn't changing any thing that Task.but Owner (Giri) getting mails is automatic task completion mail.

So how we can acheive them..

Thanks

 


 
Tony GonzalezTony Gonzalez
What if we simply wanted to email all users, who modified the task, only upon status change to Completed? In other words, any User who has ever touched the task (made changes) should get an email that the Task has now been completed.