2

If we assign a task to someone else, we can get an automated notice when the task is completed.

For that i have written one trigger mentioned below that trigger is working fine but email is being sent to creator(When a Task is created, assigned and completed by same person, still an email is being sent to creator). but i don't want like that.could any one please help me this issue

Please find the below trigger and correct me.

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'){
       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'){
      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}, \nYour task has been Completed. Here are the details - \n\n';
      template+= 'Subject -'+tsk.Subject+' \n';
      template+= 'Status - '+tsk.Status +' \n';
      template+= 'Priority - '+tsk.Priority +' \n';
      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);
}

Edit 1

But i have one more question on the same trigger.Can you please help me on that Issue.Please find below is the my Question. Question is: 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: ABC (ABC Connections) (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: Surya Dash EFG has updated the following new task: Subject: Test of Auto Email Opportunity: test11 Status - Completed Priority: Normal For more details, click the following link: my.salesforce.com/012C0000000QV4K

Edit 2

Hi Team, I have one more issue on this same Trigger ,That is how we need to restrict the email notifications to only those tasks created by the specific roles/profiles .

  • 1
    If i am getting you correctly you want the email to be sent to the creator of the task(The person who assigned the task to someone else)?? – Mr.Frodo Apr 29 '16 at 18:03
2

This is happening because of the filter conditions being used for selecting the OwnerIds and tasks you're creating emails for. You're not filtering to determine on which tasks there's been a change of ownership. Instead, you're simply adding all the createdByIds to the set of OwnerIds where the task in trigger.old wasn't complete but is now complete in trigger,new. You do this again on the task itself when you create the emails.

Your if conditions needs to look like what's below:

if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && 
   tsk.OwnerId != tsk.CreatedById){

Using the above you get the additional criteria you need to determine if there's been a change of ownership of the task which should solve your problem. Use this same criteria for the if condition in both of your for loops.

EDIT

In response to your comments regarding the template not producing the expected results, you've not posted the desired fields it should include. From what I can see, the one very obvious thing is I don't see where you're adding the tsk.Id field to your template. Without that, it would seem to be somewhat difficult for the user to have a "lookup" link to the actual task you're telling them has been completed.

Edit 2

Answer to question in 2nd edit. I would hope it would be somewhat obvious from the other answers that you need to add another if condition to test for this. In this case, you'll need to query for the profiles and roles of the createdById of users from trigger.old. Run a query on the createdById you've filtered in the 1st if condition using a WHERE clause for the profiles and roles you want them to have. Then add && queryResults.Id contains(task.createdByID) to your 2nd If condition.

  • Hi Thanks for your Help, As of now that is working as expected,But i have one more question on the same trigger.Can you please help me on that Issue.Please find below is the my Question. Question is: Task Email content does not reference to actual task.I got below email, which looks to be not correct – shrinivas May 2 '16 at 7:28
  • -----Original Message----- From: ABC (ABC Connections) Sent: Friday, April 29, 2016 4:42 PM To: ABC (ABC Connections) (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: Surya Dash EFG has updated the following new task: Subject: Test of Auto Email Opportunity: test11 Status - Completed Priority: Normal For more details, click the following link: my.salesforce.com/012C0000000QV4K – shrinivas May 2 '16 at 7:31
  • 1
    @shrinivas Glad to hear it & you're welcome. Community etiquette is to help others by marking your question as solved through checking the answer that resolved your issue. If you don't, it leaves the impression to anyone who searches the topic later that your question wasn't answered to your satisfaction. Once you have sufficient reputation, you can also upvote posts and answers. – crmprogdev May 3 '16 at 13:48
  • 1
    I would hope it would be somewhat obvious you need to add additional conditions to your if conditions and create a collection of Ids for users from your createdbyIds, then query on those roles/proflles. See my edited answer. If you have additional questions about this trigger, please open a new question. – crmprogdev May 4 '16 at 12:56
  • 1
    I have no idea what profiles and roles you're speaking of. You have general directions on how to do it. I won't be helping you learn by writing detailed code for you. You need to learn to do it for yourself. Give it a try and if you're unable to get it sorted, you can create a new question containing your current code that explains what you've tried and where you're stuck. I've answered all your questions and gone the extra mile. Best of luck to you. – crmprogdev May 4 '16 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.