7

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!

7

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.

  • As I'm using the same page for previewing inside SF and the functionality for sending out the email is already built, I thought it'll be easier just extending it with a batch or something but I reckon the VF Email Template will be the win here... Thanks Joey and @jkraybill – Boris Bachovski Aug 20 '12 at 1:17
  • @Joey Chan : Will the getContent work even for VF page with Jquery Charts? ie I have VF page which generate JQuery Charts, so If I try to programmatically access the VF page (in order to email them) will the JS/JQuery also execute and then I will get content or the content will be wo any JS execution? – Chirag Mehta Jan 10 '13 at 12:45
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/

  • 3
    +1 this works well, either this or Apex REST. – Andrew Fawcett Apr 17 '14 at 0:33
  • Since the Spring'15 release, getContentAsPDF() will be counted as a callout. We enabled the critical update and now our logic to generate the PDF from Batch or Future using Apex REST services don't work any longer. We get the error that 'Callout Loop not allowed' which makes sense we are doing a callout from a callout function. Is there any way to fix this? – RajeshShah Aug 25 '15 at 9:06
4

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

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.