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
BobBob 

Activity Trigger not working

I have a trigger that should send an email to a specific email address if the task subject is troubleshooting and it is created by a specific user. But it doesnt work. any help would be greatly appreciated as I can not figure out this issue . The code is below.

 
trigger  Trigger_SendPartsTechEmail on Task (before update) {
   List<Messaging.SingleEmailMessage> emailList=new List<Messaging.SingleEmailMessage>();
   Map<Id, User> userMap = new Map<Id,User>([SELECT id FROM User WHERE Username IN('bpoliquin@cybexintl.com.cybextest','tghetti@cybexintl.com') AND isActive=True]);
    for(Task tsk : Trigger.New){
        if(tsk.Subject=='Troubleshooting' && userMap.keySet().contains(tsk.createdById) ){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'bpoliqui@aol.com'};
            mail.setToAddresses(toAddresses); // Set the TO addresses
            mail.setSubject('A task owned by you has been updated'); // Set the subject
                String template = 'Hello {tsk.First_Name__c}, \nYour task has been modified. Here are the details - \n\n';
                template+= 'Subject - {tsk.Subject}\n';
                template+= 'Comments - {tsk.Activity_Comment__c}\n';
             mail.setPlainTextBody (template);
             emailList.add(mail);
        }
    }
    Messaging.SendEmail(emailList);
}

 
Vivek DeshmaneVivek Deshmane
Hi Bob,
Please try below code and let me know if it helps you.

trigger  Trigger_SendPartsTechEmail on Task (before update) {
   List<Messaging.SingleEmailMessage> emailList=new List<Messaging.SingleEmailMessage>();
   Map<Id, User> userMap = new Map<Id,User>([SELECT id FROM User WHERE Username IN('bpoliquin@cybexintl.com.cybextest','tghetti@cybexintl.com') AND isActive=True]);
    for(Task tsk : Trigger.New){
        if(Trigger.oldMap.get(tsk.id).Subject!=Trigger.newMap.get(tsk.id).Subject && tsk.Subject=='Troubleshooting' && userMap.containsKey(tsk.createdById) )
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'vivek.deshmane@gmail.com'};
            mail.setToAddresses(toAddresses); // Set the TO addresses
            mail.setSubject('A task owned by you has been updated'); // Set the subject
                String template = 'Hello'+ tsk.First_Name__c+', \nYour task has been modified. Here are the details - \n\n';
                template+= 'Subject -'+ tsk.Subject+'\n';
                template+= 'Comments -'+ tsk.Activity_Comment__c+'\n';
             mail.setPlainTextBody (template);
             emailList.add(mail);
        }
    }
 
 if(!emailList.isEmpty())
 {    
    Messaging.SendEmail(emailList);
 }
}
Please mark best anser if it's resovle your proble.

Regards,
-Vivek
vivek.deshmane@gmail.com
Vivek DeshmaneVivek Deshmane
Hi  Bob,
Please check if email relay setting is activited for perticular domain and your trying to send email on different.
To off email realy setting follow below step.

Got to setup ==>Email Relay Activation
Mark "Restrict Relay To Domains" uncheck then email will come if there is any domin check applied

Please let me know if it helps.
Best Regards,
-Vivek
BobBob
Currently all checkboxes are unchecked for the Email Relay Activation.
Ravi Dutt SharmaRavi Dutt Sharma
Bob, any specific reason that you have opted for trigger instead of workflow?
BobBob
Because you can’t send an email when you create a workflow for tasks.