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
sam_Adminsam_Admin 

Track email from activity history when using custom detail button

I have a custom button on lead detail page, so we send lead details through this button to a contact, how do i track the email in activity history on lead record? here is my class

    /*Controller to send This lead to any selected Contact */
    
    public class EmailController {
        public Lead leadRecord {get;set;}   
        public String TemplateID {get;set;}
        public String subject { get; set; }
        public String body { get; set; }
        public String htmlBody { get; set; }          
        public EmailTemplate  ET{ get; set; }
        public EmailController(ApexPages.StandardController controller) {
            String lid = '';
            TemplateID = ''; 
            htmlBody ='';
            if(ApexPages.currentPage().getParameters().get('id') != null )
                lid = ApexPages.currentPage().getParameters().get('id');
             system.debug('lidid ' + lid );    
                
            try{             
                leadRecord = [select Status,ID,Contact__c,Name,Company,NumberOfEmployees,Street,City,State,PostalCode,Country,Website,Salutation,FirstName,
                             LastName,Title,Department__c,Email,Phone,MobilePhone,Fax,Product__c,Industry,Specialty__c,Rating,BuyingTimeframe__c,LeadSource,WebFormDetail__c,
                             Description,Marketing_Comments__c from Lead where id = : lid];
                //Change the Template name below.
                ET = [Select ID,HTMLValue,Subject from EmailTemplate  where   name = 'Lead Email' ];            
                if (ET != null){
                    TemplateID  = ET.id;        
                    htmlBody  = ET.HTMLValue ;
                    Subject = ET.Subject;
                    ReplaceTags();   
                }
            }
            catch(Exception e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,e.getMessage()));
            }
        }                  
        
        public void ReplaceTags(){
            
            if(htmlBody.indexOf('{!Lead.Id}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Id}',nullToString(leadRecord.Id));
                    
            if(htmlBody.indexOf('{!Lead.Name}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Name}',nullToString(leadRecord.Name));
                        
            if(htmlBody.indexOf('{!Lead.Company}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Company}',nullToString(leadRecord.Company));
            
            if(htmlBody.indexOf('{!Lead.NumberOfEmployees}') >-1)
                htmlBody = htmlBody.replace('{!Lead.NumberOfEmployees}',nullToString(String.valueOf(leadRecord.NumberOfEmployees)));
                
            if(htmlBody.indexOf('{!Lead.Address}') >-1){
                String Address = '' ;
                Address =  Address  + nullToString(String.valueOf(leadRecord.Street));
                Address =  Address  + ' ' + nullToString(leadRecord.City);
                Address =  Address  + ','+ nullToString(leadRecord.State);
                Address =  Address  + ' ' + nullToString(leadRecord.PostalCode);
                Address =  Address  + ' ' + nullToString(leadRecord.Country);
                htmlBody = htmlBody.replace('{!Lead.Address}',Address);
                
            }
            if(htmlBody.indexOf('{!Lead.Street}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Street}',nullToString(String.valueOf(leadRecord.Street)));
                
            if(htmlBody.indexOf('{!Lead.City}') >-1)
                htmlBody = htmlBody.replace('{!Lead.City}',nullToString(leadRecord.City));
            
            if(htmlBody.indexOf('{!Lead.State}') >-1)
                htmlBody = htmlBody.replace('{!Lead.State}',nullToString(leadRecord.State));
                
            if(htmlBody.indexOf('{!Lead.PostalCode}') >-1)
               htmlBody = htmlBody.replace('{!Lead.PostalCode}',nullToString(leadRecord.PostalCode));
               
            if(htmlBody.indexOf('{!Lead.Country}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Country}',nullToString(leadRecord.Country));
                
            if(htmlBody.indexOf('{!Lead.Website}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Website}',nullToString(leadRecord.Website));
                
            if(htmlBody.indexOf('{!Lead.Salutation} ') >-1)
                htmlBody = htmlBody.replace('{!Lead.Salutation} ',nullToString(leadRecord.Salutation) );
                
            if(htmlBody.indexOf('{!Lead.FirstName} ') >-1)
                htmlBody = htmlBody.replace('{!Lead.FirstName} ',nullToString(leadRecord.FirstName) );
                
            if(htmlBody.indexOf('{!Lead.LastName}') >-1)
                htmlBody = htmlBody.replace('{!Lead.LastName}',nullToString(leadRecord.LastName));
                
            if(htmlBody.indexOf('{!Lead.Title}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Title}',nullToString(leadRecord.Title));
                
            if(htmlBody.indexOf('{!Lead.Department__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Department__c}',nullToString(leadRecord.Department__c));
            
            if(htmlBody.indexOf('{!Lead.Email}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Email}',nullToString(leadRecord.Email));
                
            if(htmlBody.indexOf('{!Lead.Phone}') >-1)
               htmlBody = htmlBody.replace('{!Lead.Phone}',nullToString(leadRecord.Phone));
               
            if(htmlBody.indexOf('{!Lead.MobilePhone}') >-1)
                htmlBody = htmlBody.replace('{!Lead.MobilePhone}',nullToString(leadRecord.MobilePhone));
                
            if(htmlBody.indexOf('{!Lead.Fax}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Fax}',nullToString(leadRecord.Fax));
                
            if(htmlBody.indexOf('{!Lead.Product__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Product__c}',nullToString(leadRecord.Product__c));
                
            if(htmlBody.indexOf('{!Lead.Industry}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Industry}',nullToString(leadRecord.Industry));
                
            if(htmlBody.indexOf('{!Lead.Specialty__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Specialty__c}',nullToString(leadRecord.Specialty__c));
                
            if(htmlBody.indexOf('{!Lead.Rating}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Rating}',nullToString(leadRecord.Rating));
                
            if(htmlBody.indexOf('{!Lead.BuyingTimeframe__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.BuyingTimeframe__c}',nullToString(leadRecord.BuyingTimeframe__c));
                
            if(htmlBody.indexOf('{!Lead.LeadSource}') >-1)
                htmlBody = htmlBody.replace('{!Lead.LeadSource}',nullToString(leadRecord.LeadSource));
                
            if(htmlBody.indexOf('{!Lead.WebFormDetail__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.WebFormDetail__c}',nullToString(leadRecord.WebFormDetail__c));
                
            if(htmlBody.indexOf('{!Lead.Description}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Description}',nullToString(leadRecord.Description));
                
            if(htmlBody.indexOf('{!Lead.Marketing_Comments__c}') >-1)
                htmlBody = htmlBody.replace('{!Lead.Marketing_Comments__c}',nullToString(leadRecord.Marketing_Comments__c));
                        
        }
        public PageReference send() {
         List<Messaging.SingleEmailMessage> email = new List<Messaging.SingleEmailMessage>();
         
            if(leadRecord.Contact__c == null){
                 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Please select contact and Try again.'));
                 return null;   
            }
         
            if (TemplateID != null){
                // Query Contact Email ID
                 try{
                    Contact c =  [Select Id,Email,Name from Contact where ID =: leadRecord.Contact__c]; 
                    if (c.Email != null && c.Email != ''){ 
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                            
                            if(htmlBody.indexOf('{!Contact.Name}') >-1)
                                htmlBody = htmlBody.replace('{!Contact.Name}',nullToString(c.name));
                            
                            mail.setTargetObjectId(c.id);
                            mail.setSubject(Subject);
                            mail.setHtmlBody(HtmlBody);
                         // mail.setWhatId('{lead.id}');                     
                        email.add(mail);
                        Messaging.sendEmail(email);
                        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email sent successfully!'));
                        
                        leadRecord.Status = 'Tracking';
                        update leadRecord;
                    }
                    else{
                        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Selected Contact has no Valid Email Address, kindly update the contact and Try again.'));
                        return null;   
                    }
                 }catch(Exception e){}
            }
                    
            return new PageReference('/' + leadRecord.id);  
        }
        public String nullToString(String s){
            if (s== null)
                return '';
            else 
                return s;           
        }
        public PageReference cancel(){
            return new PageReference('/' + leadRecord.id);  
        }
    }