• Emily Davis 13
  • 5 Points
  • Member since 2015

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 4
Hello all,

I have a requirement to send a VF page as a PDF attachment and also add the PDF attachment to the related Opportunity in Salesforce.  This needs to be done via a public facing Force.com site.  

The site has a VF page that is a form for a customer to fill out, and the submit button performs an update to the Salesforce records(Account, Contacts, Opportunity, and a Custom Object) with the customers input.  The generateContractPdf() method below is then called to fulfill the PDF requirement mentioned above, which is a PDF formatted version of the form the customer filled out.

I am able to fulfill both requirements when running as an Admin, but through the Force.com site, the PDF attachment is created but will not open.("Failed to load PDF" or "format error: not a PDF or corrupted" error).  

If I remove the @future annotation, the PDF loads fine, however, the updates made from the original VF page controller(customer input) are not reflected on the resulting PDF.

If I change getContent() to getContentAsPdf(), the PDF will open, but it is just a blank page.

Any help is greatly appreciated.
public static void generateContractPdf(String oppId, String contactId, String businessName, Boolean requestedEmail) {

        Blob body;
        String fileName = businessName.replaceAll(' ', '_').replaceAll(',', '_').replaceAll('//', '_').replaceAll('/', '_') + 
            + '_Service_Agreement_' 
            + System.now().format('MM/dd/yyyy HH:mm:ss','America/Denver')
            + '.pdf';

        try {

            PageReference pdf = new PageReference('/apex/ContractPDFVersion1');
			pdf.getParameters().put('id', oppId);
            body = Test.isRunningTest() ? Blob.valueOf('Test') : pdf.getContent();

            ContractForm_Controller.sendAndAttachPdf(body, fileName, oppId, contactId, requestedEmail);

        } catch (VisualforceException e) {

            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error creating the Contract PDF: ' + e,
                Error_Type__c = 'Contract PDF Creation Error',
                Opportunity__c = oppId);
            insert error;

    public static void sendAndAttachPdf(Blob body, String fileName, String oppId, String contactId, Boolean requestedEmail) {

        ContractEmailTemplate__c contractEmailTemplateID = ContractEmailTemplate__c.getValues('Services');

        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.body = body;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment }); 

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setSubject('Thank you for your business!');
        message.setPlainTextBody('Thank you for signing up');
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment });
        message.setToAddresses(new String[] { 'example@example.com' });

        try {
            if(requestedEmail) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail,message });

            } else {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });

        } catch (Exception e) {
            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error sending the Contract PDF email: ' + e.getMessage(),
                Error_Type__c = 'Contract PDF Email Error',
                Opportunity__c = oppId);
            insert error;

        try {

            Attachment accountAttachment = new Attachment();
            accountAttachment.ParentId = oppId;
            accountAttachment.Name = fileName;
            accountAttachment.Body = body;
            insert accountAttachment;

        } catch (Exception e) {

            Error_Log__c error = new Error_Log__c(
                Error__c = 'There was an error attaching the Contract PDF to the Opportunity: ' + e,
                Error_Type__c = 'Contract PDF Attachment Error',
                Opportunity__c = oppId);
            insert error;

  • October 23, 2015
  • Like
  • 0
Hi there

Salesforce does not allow you to create PDF attachments in batch job.

This is work around to that.

It works!

But As of Summer 15, Salesforce have implemented a critical update "PageReference getContent() and getContentAsPDF() Methods Treated as Callouts". Once enabled, you may get the rather uninformative error "(304497466)|FATAL_ERROR|System.CalloutException: Callout loop not allowed".

And so I have the problem described in this blog: http://codrspace.com/gwickman/callout-loop-not-allowed-error-when-using-getcontent-and-getcontentaspdf/

Can someone please help me get around this error or help with another way to create PDF attachments for emails in a batch job?

Thank you very much