You need to sign in to do that
Don't have an account?
Joe Hayes
Send email even with no attachments
Hi Everyone,
I have a class that grabs file attachments and attaches them to an email along with a set document and then sends the email.
I am looking to modify this code so that it will grab attachments if there are any, but if there are no attachments only add the set document and send the email,
I have had a look at this but cannot figure out how to get this, I guess it will be if attachment size is > 1 then do this. If not do that.
Would anyone be able to have a look?
Hope that all makes sense,
Thanks
Joe
I have a class that grabs file attachments and attaches them to an email along with a set document and then sends the email.
I am looking to modify this code so that it will grab attachments if there are any, but if there are no attachments only add the set document and send the email,
I have had a look at this but cannot figure out how to get this, I guess it will be if attachment size is > 1 then do this. If not do that.
Would anyone be able to have a look?
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); List<String> toaddress = new List<String>(); toaddress.add(string.valueof(cf.Email_to_send_confirmation_to__c)); mail.settoaddresses(toaddress); mail.setReplyTo('trainingbookings@certsure.com'); mail.setSenderDisplayName('Certsure Training'); mail.setBccSender(false); mail.setUseSignature(false); mail.saveAsActivity = false; mail.setSubject(subject); mail.setHtmlBody(htmlBody); //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); } // for the T&C's Document doc = [select id, name, body, contenttype, developername, type from Document where Name = 'Certsure_Training_Terms_And_Conditions'][0]; Messaging.Emailfileattachment MyDocefa = new Messaging.Emailfileattachment(); MyDocefa.setContentType(doc.contentType); MyDocefa.setFileName(doc.developerName+'.'+doc.type); MyDocefa.setBody(doc.Body); fileAttachments.add(MyDocefa); mail.setFileAttachments(fileAttachments); //Send email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Hope that all makes sense,
Thanks
Joe
Thanks for the reply, I think that should work great :)
I am getting an error further down my class where it copies the attachment ID onto my custom object.
I'm not quite sure where I've gone wrong but I'm getting the error:
"List has no rows for assignment to SObject
Error is in expression '{!emailAtt}' in component <apex:commandButton> in page sendtrainingconfirmation: Class.TrainingConfirmationSend.emailAtt: line 92, column 1"
The following code starts at line 90:
Would you be able to see where the problem lies with this?
If possible id also like to select Id from Attachment where ParentId = :oppr AND Filename ends with ".pdf" - is this possible?
Thanks for your help with this it is very much appreciated.
Kind regards
Joe
Can you please send me the VF page and the controller.
Thanks
My Controller is:
And My VF Page is very simple:
Thanks,
Swaraj
I'm getting a Variable does not exist: alstattach.id error on line 95, but I cannot see why!
Do you know?
Thanks
Joe