You need to sign in to do that
Don't have an account?
Joe 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...
Thanks for your help,
Kind regards
Joe
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
You have declared address inside GetCustomFields(); which you cant use outside that method. Try this code:
All Answers
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.
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
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));
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
You have declared address inside GetCustomFields(); which you cant use outside that method. Try this code:
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?
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');
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