1

I have two objects. Department__c and Employee__c. They have master detail relationship. Department__c has a field called 'Active__c' - a checkbox. Whenever the value of that checkbox changes, I am firing an after update trigger that sends email to employees in that department. There is no error shown while update but email is not sent either. Please help. Here is the trigger code and handler code respectively.

Trigger

trigger DeptActiveUpdate on Department__c (after update) {
    String[] strList=new List<String>();     
    list<Employee__c> lstEmployee=new list<Employee__c> ([select email__c, department__c from Employee__c where Department__c in :Trigger.New]);   
    for (Department__c dept: Trigger.new)   
    {       
        Department__c objOldDept=Trigger.oldMap.get(dept.ID);
        if(dept.Active__c != objOldDept.Active__c)
        {             
        for( Employee__c emp: lstEmployee)
        {
            if(dept.Id == emp.department__c )
            strList.add(emp.email__c);           
        }
        }     
            if(dept.Active__c == true)
            {
                 EmailSender.activeTrue(strList);
            }
            else
            {
                 EmailSender.activeFalse(strList);
            }                   
        }         
}

Handler

public class EmailSender{            
    public static void activeTrue(List<String> str){
        System.debug('Email List: '+str);
        Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
        mail1.setSubject('Important');
        mail1.setPlainTextBody('This department has been activated.');
        mail1.setSaveAsActivity(false);
        mail1.setToAddresses(str);
        User objUser= [select id from User where Email = 'jigartrivedi88@yahoo.com' limit 1];
        mail1.setTargetObjectId(objUser.Id);
        Messaging.sendEmail(new Messaging.Email[] {mail1});
        System.debug('inside active true. end of function.');
    }    
    public static void activeFalse(List<String> str){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Important');
        mail.setPlainTextBody('This department has been activated.');
        mail.setSaveAsActivity(false);
        mail.setToAddresses(str);
        User objUser = [select id from User where Email = 'jigartrivedi88@yahoo.com' limit 1];
        mail.setTargetObjectId(objUser.Id);
        Messaging.sendEmail(new Messaging.Email[] {mail});        
        System.debug('inside active false. end of function.');
    }
}
  • Did you try using Work Flow rule for the same purpose? – snehakem May 4 '15 at 9:23
  • No. I have been asked to build trigger not work flow. – Jigar Trivedi May 4 '15 at 9:30
  • Did you check in debug logs? – snehakem May 4 '15 at 9:31
  • Yes. the list of emails passes correctly. send mail method doesn't work. – Jigar Trivedi May 4 '15 at 9:32
  • 1
    Check this setting, Setup--->Email Administration--->Deliverability--->Access level=All Emails – snehakem May 4 '15 at 9:36
-1

It seems that yahoo policy is conflicted. As I am sending email through my yahoo account as a user. What was happening and what made my above code correct is I was sending mails to multiple employees using my user account in salesforce which has yahoo mail id. When the trigger was fired the code was running properly but the mail was not sent to the intended recipients. It was due to Yahoo's policy. And, as a result the mail was failed to send. I am executing it in my developer account.

| improve this answer | |
  • jigar, what I believe everyone is saying: that's a very short answer. If you would be able to extend it with usefull information that could help others in the future, your answer would be of much benefit to this community. How did you succesfully troubleshoot this, and what have you changed to make it work ? – Samuel De Rycke May 4 '15 at 16:35
  • Okay, I am new to this community so didn't know I shouldn't thank the people that spared their time and helped me. What was happening and what made my above code correct is I was sending mails to multiple employees using my user account in salesforce which has yahoo mail id. When the trigger was fired the code was running properly but the mail was not sent to the intended recipients. It was due to Yahoo's policy. And, as a result the mail was failed to send. I am executing it in my developer account. – Jigar Trivedi May 5 '15 at 5:22
  • not sure if Yahoo policy would be applied here, maybe the daily limit of emails would have hit, as for developer edition it is very less? – Aditya Vijay Apr 15 at 10:11

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.