You need to sign in to do that
Don't have an account?
Calling getContent() from a trigger
Three days after a particular condition is met, I need to send an email message that includes a PDF attachment. I've set up workflow to do a field update when the time-based condition is met. I have a trigger that detects the field update and generates the email message.
To generate the PDF attachment, the trigger has to get a PageReference to the VF page, and then call either getContent() or getContentAsPDF(). Unfortunately, these methods aren't supported in a trigger.
[Actually, the documentation says that getContent() isn't supported in a trigger, but it doesn't say the same thing about getContentAsPDF(). It makes sense that, if getContent() isn't supported, neither is getContentAsPDF(), but the documentation should say so. But I digress.]
Since the documentation doesn't say anything about getContent() not being supported with @future methods, I wrote an @future method that generates the PDF and sends the email, and changed my trigger to call that. But apparently getContent() doesn't work from an @future method.
[Interestingly, getContent() fails differently when called from a trigger than it does when called from @future. When called from a trigger, getContent() throws an exception. When called from an @future method, getContent() doesn't fail -- it just generates a blank PDF, which is the same the thing doc says it does when called from a unit test. But again, I digress.]
Because of the time-based nature of the condition that causes the email to be sent, I need to use workflow to detect the condition. The PDF attachment is complex enough that it needs its own VF controller, so I can't use a messaging:attachment, and I can't use a workflow email action. I need to use a trigger to send the email. Any suggestions on how I can get a trigger to construct and send this email message with its attachment?
I have managed to get a work around by using a http req call to a sites page that creates the PDF, attaches it to the object and emails it to the current user, by doing the getcontent call from the controller. You just need to pass the parameters you want to the sites page.
This is an example of the call to a sites page passing 2 parameters, the ID of the object and the current users ID. Don't forget to make sure you add your sites domain to the Remote Access list and that your sites guest user has access to the fields and objects that it is creating the PDF from
The Trigger
The class
but how did you attach it since there is no getContent on HttpResponse. Simply casting the result.getBody() to a blob doesn't work. I get an empty pdf.. Any ideas?
Please post your work around for this if you found one as I am facing the same issue currenlty
http://www.shivasoft.in/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/
Trigger -> Asynch Method -> REST based Apex class -> Send Email
Thanks,
Jitendra Zaa