You need to sign in to do that
Don't have an account?
KRISHNAMURTHY KOLUMAM ANANTHARAMAK
Pdf upload failed apex
Hi
I am trying to send an email with pdf whenever a pdf is attached in "Notes and Attachment" section. Whenever I try to upload it is giving me an error
10:04:27:005 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]
Please find the below code for Apex trigger.
I am trying to send an email with pdf whenever a pdf is attached in "Notes and Attachment" section. Whenever I try to upload it is giving me an error
10:04:27:005 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]
Please find the below code for Apex trigger.
trigger EmailTrigger on ContentVersion (after insert) { List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); for(ContentVersion cv : trigger.new) { Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); attachment.setBody(cv.ContentBodyId); attachment.setFileName(cv.Title + '.' + cv.FileType); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' }; message.subject = 'Test Mail'; message.plainTextBody = 'Attached file name: ' + cv.Title; message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); allMessages.add(message); } Messaging.sendEmail(allMessages); }Kindly help me to rectify the same.
Your code helped me to activate the trigger, but I would like to know how to tag fields from Invoice object ?
Invoice object is a custom object and the "Notes and Attachment" related list is present in invoice object.
Thanks for your immense help!
The technique is as follows.
- Create a new Visualforce page that displays the Invoice information and is formatted. This page would be ultimately rendered as a PDF. Lets call it the InvoicePdfAttachment.
- Write a second page which has the Apex Controller to generate the InvoicePdfAttachment as a pdf and attaches it to the email body and sends out an email. You will need to use the getContentAsPDF() of the Pagereference class to generate the Pdf.
Please mak the thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.I don't wanna create a new page. I just wanna tag invoice object fields in subject and body. What should I be doing here and how do I write a test class for the below code.
Once your code works as desired you can then implement the test code by inserting the relevant records i.e. Invoice & associated Content which will automatically invoke the code written on After Trigger. Only thing is you may, need to use Test.isRunningTest() in the code above to implement the branching logic for avoiding email sending and consumption of your email limits when the email sending code is invoked through Test class execution.
Thanks for your wonderful explanation. Appreciate your time on this. I have written a test code which looks like this. Kindly assist me if it's correct and let me know what changes need to be done so that it is done correctly.
It works super fine. See the below code. I am stuck with test class to deploy in production.