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
Amit SAmit S 

Need to Use a Custom Page on Send Email Button

Hi All,

Need to add Custom fields from Tasks Objects onto the Email Page once clicked on the Send Email Button.

Has anyone customized the Send email page? Is a VF Page and Custom controller the only option? Appreciate any help or Link to a similar peice of customization on this.

I have tried using window.location = '/_ui/core/email/author/EmailAuthor?p2_lkid=' + cont_result[0].Id + '&rtype=003&p3_lkid={!Account.Id}&retURL=%2F{!Account.Id}&template_id=00XN0000000QfDy'; but this allows only the Body of the email to be customized.

Where as I want Two Custom fields to be shown and allowed to pick values from the Email Page before sending.

 
pconpcon
Unfortunately the only way to do this is to use a custom Visualforce Page and controller.
Amit SAmit S
Hey pcon, Do you have any helpful links or sample piece of code?
pconpcon
Unfortunately I do not have any.  You will have to create your Visualforce page and then use the Messaging class [1] to generate your email and then generate your Task.

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_messaging.htm
Deepthi BDeepthi B
Hello Amit,

Check the below sample code thats close to your requirement. Hope it may help you!
<!-- VF Page ---->
<apex:page controller="EmailController">
 <apex:form >
     <apex:pageBlock >
         <apex:pageblockSection >
             <apex:pageblockSectionItem >      
                 <apex:outputLabel >Email Id</apex:outputLabel>
                 <apex:inputtext value="{!emailId}"/>
             </apex:pageblockSectionItem>    
             <apex:pageBlockSectionItem >
                 <apex:outputLabel >Cc Email Id</apex:outputLabel>
                 <apex:inputText value="{!CcemailId}"/>
             </apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem >
                 <apex:outputLabel >Body</apex:outputLabel>
                 <apex:inputText value="{!body}"/>
             </apex:pageBlockSectionItem>
             <apex:commandButton value="Send Email" action="{!sendEmailFunction}"/>
         </apex:pageblockSection>
     </apex:pageBlock>
 </apex:form>
</apex:page>
 
// Custom Controller Class
public class EmailController{

    public String body { get; set; }

   public String CcemailId {get; set;}

   public String emailId {get; set;}

    public EmailController(){
    }
    public void sendEmailFunction() {
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       mail.setToAddresses(new string[] {emailId});
       mail.setCcAddresses(new string[] {CcemailId});
       mail.setReplyTo('XXX@gmail.com');
       mail.setSenderDisplayName('My Name');
       mail.setSubject('Testing email through apex');
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setPlainTextBody(body);
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
    }
}
Regards,
Deepthi

 
Amit SAmit S
Thanks Deepthi,

Want to relate this email to the Account Record and be saved in the Activity History, shall the Controller be an extension to the Task Controller?
Deepthi BDeepthi B
Hi Amit,

Yes, you can add this as an extension to your TaskController class. Hope it works. 
Please let me know if required any help!

Regards,
Deepthi
Amit SAmit S
Hey Deepthi & PCon, 

I was able to build this custom Send Email page. But unable to add a add multiple attachments before sending the email. Have found a few blogs on adding custom pages to Pick Attachments but not sure how I will pass the context of the Email Page and get it back from the Multiple attachment uploader page. as the email is not yet send and does not have the Task Id yet. Appreciate any help on this.

Below is the Controller I have built so far.
 
public class EmailController{
   
   public string p3_lkid = ApexPages.currentPage().getParameters().get('p3_lkid');
   public string p2_lkid = ApexPages.currentPage().getParameters().get('p2_lkid');
   
   public String body {get; set;}
   public String CcemailId {get; set;}
   public String BCcemailId {get; set;}
   public String addlRecipients {get; set;}
   public String emailId {get; set;}
   public ApexPages.standardController st_controller;
   public Id currtaskId{get; set;}
   public Task sTask{get; set;}  
   public TaskRecordTypeRef rc {get; set;}
   public PageReference page{get; set;}
   Public Contact Con{get; set;}
   Public Contact QueryWho{get; set;}

   public EmailController(ApexPages.StandardController stdController){
       st_controller = stdController;
       currtaskId = st_controller.getId();
       stask = querytask(currtaskId);
       stask = TaskInit(stask);
    }
    
   public PageReference sendEmailFunction() {
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
       if (CcemailId != null && CcemailId != '') {mail.setCcAddresses(CcemailId.split(';'));}              
       if (BCcemailId != null && BCcemailId != '') {mail.setBCcAddresses(BCcemailId.split(';'));}       
       String[] addlToAddresses = null;
       if (addlRecipients != null && addlRecipients != '') {addlToAddresses = addlRecipients.split(';');}                     
		List<String> lstToAddresses = null;
		if (addlToAddresses != null) {			
			lstToAddresses = new List<String>(addlToAddresses);		} 
		}
       lstToAddresses.add(emailId);
	   mail.setToAddresses(lstToAddresses);       
       mail.setReplyTo(UserInfo.getUserEmail());
       mail.setSenderDisplayName(UserInfo.getFirstName() + ' ' + UserInfo.getLastName());
       mail.setSubject(stask.Subject);       
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setPlainTextBody(body);       
       
       if (attachment.Body != null) {
           Messaging.EmailFileAttachment emailAttachment = new Messaging.EmailFileAttachment();
           emailAttachment.setBody(attachment.Body);
           emailAttachment.setFileName(attachment.Name);
           mail.setFileAttachments(new List<Messaging.EmailFileAttachment> {emailAttachment});
       }
       
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       if(stask.Id == null){            
            stask.Priority = 'Normal';            
            stask.Status = 'Completed';
            stask.RecordTypeId = '01290000000tfI7';
            stask.WhatId = p3_lkid;            
            stask.Type = 'Email';
            stask.ActivityDate = Date.today();
            stask.Priority = 'Normal';
            stask.Subject = 'Email: ' + stask.Subject;
            stask.Description = 'Additional To: ' + '\nCC: ' + CcemailId + '\nBCC: ' + BCcemailId + '\nAttachment:' + attachment.Name  + '\n Subject: ' + stask.Subject + '\n Body: \n' + body;
            insert stask;
        }
       //Commented below piece of code as Attachments are not getting saved as default functionality
       /*if (attachment.Body != null) {
                    attachment.parentId=stask.Id;
                    insert attachment;
                }*/
       
       page = new PageReference('/' + stask.WhatId);
       page.setRedirect(true);
       return page;
    }
    
    public PageReference cancel() {        
        PageReference pgRef = new PageReference('/' + p3_lkid );
        pgRef.setRedirect(true);
        return pgRef;
    }
    
    public PageReference Attach() {        
        PageReference pgRef = new PageReference('/apex/MultiAttachment' );        
        pgRef.setRedirect(true);
        return pgRef;
    }
    
    public Task querytask(String queryid){
        Task tsk = new Task();
        if(queryid != null){
            String queryStr = 'Select ' + sObjectGetAllFields(tsk).trim() + ' From Task Where Id= : queryid';          
            try{
                tsk = Database.query(queryStr);
            }
            catch(Exception e){
                system.debug('****** Quering Task Exception: ' + e.getMessage());    
            }                                      
        }                
        return tsk;        
    }
    
    public Task TaskInit(Task tsk){
        rc = new TaskRecordTypeRef();
        Con = [SELECT Id, Email
                 FROM Contact WHERE Id = : p2_lkid];         
        
        QueryWho = [SELECT Id, Email
                 FROM Contact WHERE AccountId = : p3_lkid];
        
        if(tsk.Id == null){                       
            tsk.OwnerId = UserInfo.getUserId();
            tsk.WhatId = p3_lkid;
            tsk.WhoId = QueryWho.Id;            
            tsk.ActivityDate = date.today();
            tsk.Status = 'Completed';
            emailId = Con.Email;
            BCcemailId = UserInfo.getUserEmail();
        }		
        return tsk;
    }
    
     /* Retrieve all fields from the specified object */
    public String sObjectGetAllFields(SObject obj){
        String str = '';                   
        Map<String, Schema.SObjectField> map_fields = obj.getSObjectType().getDescribe().fields.getMap();
        List<Schema.SObjectField> li_fields = map_fields.values();
        
        for(Schema.SObjectField field : li_fields){
            str+= field + ',';       
        }
        
        return str.substring(0,str.length()-1);
    }
    
    public Attachment attachment {
        get {
            if (attachment==null) {
                System.debug('==========> creating new empty Attachment.');
                attachment = new Attachment();
            }
            return attachment;
        }
        set;
    }
}