Find your content:

Search form

You are here

Can't use getContent() in a batch, future method, trigger etc

 
Share

I have a VF page which has a method in the controller that generates PDF and sends it out via email.

It's all working fine when I trigger off the method from a custom button on the page, which is the manual process from inside SF.

I need to automate the same process as we have an integrated mobile app that pushes up opportunities with line items. The problem is that I need to generate the PDF once the opportunity and all related line items are in.

Basically, the opportunity is inserted, then we retrieve the ID and then we push all the line items from the mobile app. Therefore we don't know when the last line item went in.

The thing is that I don't want to use another API call to update the opportunity once we push all line items from the mobile app as we have a bit of complexity in our API methods and that will get messy.

My first approach was to create a batch that will run hourly and will pick up the new opportunities that are created from the mobile app and marked with a checkbox. Half way through I realised that getContent() method is not allowed from a batch so I can't get the PDF and send out the emails. Then I did a bit of research and saw that same method cannot be called even from a trigger or a future method or whatever.

So far I saw one solution which is creating a webservice that will generate the PDF and send the email but I need to call the webservice via HTTP request, create remote sites etc. which will also get messy.

Any idea is appreciated!


Attribution to: Boris Bachovski

Possible Suggestion/Solution #1

Since you mentioned that you're using Visualforce page to send out an email, have you considered using a VisualForce email template? You could generate the PDF using VisualForce components and have that added into the VisualForce email template as an attachment.

You may also use workflow rules to send the email to specified users.


Attribution to: Joey Chan

Possible Suggestion/Solution #2

This article should point you in the right direction using VF email attachment tags. Visualforce Sample - PDF Quotes and Email Templates


Attribution to: jkraybill

Possible Suggestion/Solution #3

In the past, we were using an HTTP request to achieve this. However, as of Winter '16, Salesforce allows calls to getContent() and getContentAsPdf() within asynchronous Apex (batch, future methods). http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_pagereference_getcontent.htm


Attribution to: Emily Davis

Possible Suggestion/Solution #4

You could also expose your Apex method as Webservice and consume this method in your trigger or Batch Apex. This webservice will be able to access getContent() method.

You can have a look on similar blog post here, I was also in need of something like this.

http://www.jitendrazaa.com/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/


Attribution to: Jitendra Zaa
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/551

My Block Status

My Block Content