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
DFDF 

Messaging.sendEmail Setting Sender Address to Last Modifier instead of Lead Owner.

A quick summary on what I'm trying to accomplish here. 

 

We built a set of workflow rules that update a particular field after the elapse of a certain period of time. This field contains the template name of the email we wish to send to the records being updated. 

 

The issue is we cannot get the Message to actually come from the Lead Record Owner. I can spoof it(setting Display Name) or even set a reply to. The sender email address however remains as the last modifier.

 

I've been struggling with this for quite some time now. Every solution I find eventually runs into one problem or the next. Any advice or help on how this can be accomplished would be greatly appreciated.

 

Thanks!

 

My code is posted below. 

 

 

trigger testingemail on Lead (before update) {
   
                Set<Id> Oid = new Set<Id> {};

                for (Integer i = 0; i < Trigger.new.size(); i++) {
                   
                        if (Trigger.new[i].Email_List__c != Trigger.old[i].Email_List__c){
                           
                            //If the trigger does not contain account exec.   
                            //if(!Oid.contains(Trigger.new[i].OwnerID)){   
                                Oid.add(Trigger.new[i].OwnerId);
                            //}
                       
                    }
                }
              
               EmailTemplate[] eid = [select e.Id,e.Name from EmailTemplate e where name=:Trigger.new[0].Email_List__c];
              
               if(Oid.size() < 10 && Oid.size() > 0){
                      for (Id o_id : Oid){
                         
                          Id[] leadList = new Id[] {};
                          for (Integer i = 0; i < Trigger.new.size(); i++) {
                               if ((Trigger.new[i].Email_List__c != Trigger.old[i].Email_List__c) && (Trigger.new[i].OwnerId == o_id)){
                                  
                               leadList.add(Trigger.new[i].Id);
                          
                               }
                          }
                          if(LeadList.size()!=0)
                          {
                          User [] umail = [Select u.Email,u.Name from User u where id=:o_id];
                             
                          Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                         
                          mail.setTargetObjectIds(leadList);
                   
                        mail.setTemplateId(eid[0].Id);
                       
                        mail.setReplyTo(umail[0].Email);
                        mail.setSenderDisplayName(umail[0].Name);
                       
                        Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
                          }
                         
                       }   
                      
                      
                      
                      
               }   
}       

HadasHadas

Hi DF,

 

Did you find a solution to your problem?

I am facing the same issue...

Is there a way to set the sender email?

 

Thanks,

Hadas