• LouisSkelton
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hello all!

 

This is my first foray into the world of Apex coding. I'm not very famiiliar with coding so appologies in advance for simple errors!

 

I have put the below trigger together piecing various bits of code accross various forums together to trigger an email to the creator of a task on completion if that task is assigned to another person. It works great! However, the user gets two emails each time - is there a way around this? I did a search on the forum and coultn't find a previous thread which shared similat code to my own.

 

Thanks!

 

Louis

 

Here's the code:

 

trigger Trigger_Request_CompletedAfterInsertAfterUpdate on Task (after update)
 {
    
 
   Task[] completedTasks = Trigger.new;
   for(Task t : completedTasks){
      if(t.OwnerId!=t.CreatedById && t.Status=='Completed'){
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();      

    mail.setTargetObjectId(t.CreatedById);          

    mail.setSubject('Task Completed');        

    mail.setPlainTextBody(Datetime.now().format('MM/dd/yyyy hh:mm a')+' \r\n'+t.Description+' \r\n'+' \r\n'+'Salesforce link: \r\n'+'https://emea.salesforce.com/'+t.AccountId);
    mail.saveAsActivity = false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
      }
   }
}

Hello all!

 

This is my first foray into the world of Apex coding. I'm not very famiiliar with coding so appologies in advance for simple errors!

 

I have put the below trigger together piecing various bits of code accross various forums together to trigger an email to the creator of a task on completion if that task is assigned to another person. It works great! However, the user gets two emails each time - is there a way around this? I did a search on the forum and coultn't find a previous thread which shared similat code to my own.

 

Thanks!

 

Louis

 

Here's the code:

 

trigger Trigger_Request_CompletedAfterInsertAfterUpdate on Task (after update)
 {
    
 
   Task[] completedTasks = Trigger.new;
   for(Task t : completedTasks){
      if(t.OwnerId!=t.CreatedById && t.Status=='Completed'){
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();      

    mail.setTargetObjectId(t.CreatedById);          

    mail.setSubject('Task Completed');        

    mail.setPlainTextBody(Datetime.now().format('MM/dd/yyyy hh:mm a')+' \r\n'+t.Description+' \r\n'+' \r\n'+'Salesforce link: \r\n'+'https://emea.salesforce.com/'+t.AccountId);
    mail.saveAsActivity = false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
      }
   }
}