+ Start a Discussion

Issue with emailing PDF attachment



I have a Detailed Page Button, which onClick runs JavaScript and calls out a method inside a global class. Here is the code for that:



sforce.apex.execute( "EmailInvoiceController", "emailinvoice", {invid:"{! Invoice__c.Id }"});


The global class EmailInvoiceController is as follows.


global class EmailInvoiceController
    webservice static void emailinvoice(ID invid)
// code which fetches the Visualforce page and emails a pdf

It works perfectly fine and i get an email with a pdf attachment when the button is clicked. Now i run a trigger that calls this method every time a record is inserted. The problem i am facing here is that the email that i receive due to the trigger has a pdf attachment which is malformed and cannot be opened. Also the size of the attachment is reduced to 1.1 kb from the expected 20 kb.


I have not changed the code whatsoever and the same code gives perfect results on button click, but not when the method is called from a trigger.


Any directions on this issue are highly appreciated.


Thanks !!

- P 




I'm guessing you're using getContentAsPDF. From the Apex Developers Guide:



The getContent and getContentAsPDF PageReference methods aren't allowed in triggers.



I don't think what you're trying to do is possible. You could include a URL in the email that would open the PDF when the user clicks on it, but no PDF attachments.






Thank you for your reply. Yes, i am using getContent to get the data. So here, instead of using a trigger if i schedule this class as a cron job, will it work? I suppose i will be getting the same error.


- P