• Beau Anderson 43
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.
 
@future(callout=true) 
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.setContentType('application/pdf');
        attachment.setFileName(fileName);
        attachment.setInline(false);
        attachment.body = body;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setUseSignature(false);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment }); 
        mail.setTemplateId(contractEmailTemplateID.TemplateID__c);
        mail.setTargetObjectId(contactId);

        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