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
Joe HayesJoe Hayes 

Class to send email with attachments from ParentID

Hi,

I am writing a class to send an email with all attachments related to record.

I have used bits and samples from a few different places and managed to get the attachment side working fine. 
I am having trouble setting the toaddresses. Instead of a fixed address it needs to reference a field called Email_to_send_confirmation_to__c.

I dont know why but I can't figure out how to do it.

Please could someone have a look below and see where I am going wrong. I also believe the code is quite messy as I have cut and pasted bits from all over...
 
public class TrainingConfirmationSend {
    ApexPages.StandardController controller;   
 
 public course_sale__c q
 {get;set;}
    
 String op = ApexPages.currentPage().getParameters().get('id');
 
 public Id oppr   
 {get;set;}       
 
 public TrainingConfirmationSend(ApexPages.StandardController ctrl)   
 {oppr = ctrl.getRecord().Id;}
     
 public void GetCustomFields() {
            course_sale__c address = [SELECT Email_to_send_confirmation_to__c FROM Course_Sale__c WHERE id = :oppr];
  }
 public PageReference emailAtt()
 {
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     string[] toaddress = New String[] {address.Email_to_send_confirmation_to__c};
        mail.settoaddresses(toaddress);
        mail.setReplyTo('test@test.com');
        mail.setSenderDisplayName('test');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setTemplateId('00XD0000001dOXr');
        mail.saveAsActivity = false;    
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
}
    
}

Thanks for your help,

Kind regards
Joe
Best Answer chosen by Joe Hayes
Sudhir ChowdarySudhir Chowdary
Hi,

You have declared address inside GetCustomFields(); which you cant use outside that method. Try this code:
public class TrainingConfirmationSend {
    ApexPages.StandardController controller;   
 
 public course_sale__c q
 {get;set;}
    
 String op = ApexPages.currentPage().getParameters().get('id');
 
 public Id oppr   
 {get;set;}  

Public course_sale__c address; 
 
 public TrainingConfirmationSend(ApexPages.StandardController ctrl)   
 {oppr = ctrl.getRecord().Id;}
     
 public void GetCustomFields() {
            address = [SELECT Email_to_send_confirmation_to__c FROM Course_Sale__c WHERE id = :oppr];
  }
 public PageReference emailAtt()
 {
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	GetCustomFields();
     List<String> toaddress = new List<String>();
	 toaddress.add(string.valueof(address.Email_to_send_confirmation_to__c));
        mail.settoaddresses(toaddress);
        mail.setReplyTo('test@test.com');
        mail.setSenderDisplayName('test');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setTemplateId('00XD0000001dOXr');
        mail.saveAsActivity = false;    
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
}
    
}

All Answers

Sudhir ChowdarySudhir Chowdary
Hi,

If you want to add to address manually use List<String> toaddress = new List<String> { 'emailaddress' }; insted of string[] toaddress = New String[] {address.Email_to_send_confirmation_to__c};

in emailaddress place enter email to whic you want to send email.
Joe HayesJoe Hayes
Hi Sudhir,

Thanks for getting back to me, the problem is that each time the class runs the email address will be different, it will be taken from a field on the object. (Email_to_send_confirmation_to__c)
I'm just not sure how to set that variable.

Thanks
Joe
Sudhir ChowdarySudhir Chowdary
Hi,

Are you sure your are calling GetCustomFields() method if so 

Try this:
 List<String> toaddress = new List<String>();
toaddress.add(string.valueof(address.Email_to_send_confirmation_to__c));
Joe HayesJoe Hayes
Thanks Sudhir,

I'm getting "Variable does not exist: address.Email_to_send_confirmation_to__c" error now.
I'm not sure have set the variables correctly at all, im confused :s

Thanks for your help
Sudhir ChowdarySudhir Chowdary
Hi,

You have declared address inside GetCustomFields(); which you cant use outside that method. Try this code:
public class TrainingConfirmationSend {
    ApexPages.StandardController controller;   
 
 public course_sale__c q
 {get;set;}
    
 String op = ApexPages.currentPage().getParameters().get('id');
 
 public Id oppr   
 {get;set;}  

Public course_sale__c address; 
 
 public TrainingConfirmationSend(ApexPages.StandardController ctrl)   
 {oppr = ctrl.getRecord().Id;}
     
 public void GetCustomFields() {
            address = [SELECT Email_to_send_confirmation_to__c FROM Course_Sale__c WHERE id = :oppr];
  }
 public PageReference emailAtt()
 {
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	GetCustomFields();
     List<String> toaddress = new List<String>();
	 toaddress.add(string.valueof(address.Email_to_send_confirmation_to__c));
        mail.settoaddresses(toaddress);
        mail.setReplyTo('test@test.com');
        mail.setSenderDisplayName('test');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setTemplateId('00XD0000001dOXr');
        mail.saveAsActivity = false;    
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
}
    
}
This was selected as the best answer
Joe HayesJoe Hayes
Hi Sudhir,

Thanks, that has got it I think,

I am getting a different error when sending now because I havent added a targetObjectId. "REQUIRED_FIELD_MISSING, Missing targetObjectId with template"

I can't really set this i don't think as the email isnt related to a contact or lead..

Do you have any ideas on this?
 
Sudhir ChowdarySudhir Chowdary
Hi Joe,

Insted of email template set body in class only. Remove mail.setTargetObjectId(userId); and use:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setHtmlBody('Hi, <br/>Please find attached estimates');
Joe HayesJoe Hayes
Hi Sudhir,

I think that has got it!!! I am going to do a bit of testing now and make sure it works as it should.


Thanks so much for your help :)

Thanks
Joe
Sudhir ChowdarySudhir Chowdary
You are welcome Joe, mark it as best answer if it does help you.