function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WEN JIEWEN JIE 

How to add a standard page content in mail attachment

Hi,

 

So far I can get a visualforce page content as pdf by pageRef.getContentAsPdf() method.

 

Now I want to get a standard page content and send them as pdf attachment in my mail.

I use the following code:

Audit__c audit = new Audit__c();
audit.Id = auditId;
        
PageReference pageRef = new ApexPages.StandardController(audit).view();
pageRef.s  etRedirect(true);

Blob b = pageRef.getContent();

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.pdf');
efa.setContentType('application/pdf');
efa.setBody(b);

But when I try to open this pdf file I get some error as "wasn't correctly decoded".

 

If I use pageRef.getContentAsPdf(), the code can't go through and doesn't have any error info.

 

So does anyone know how to change a standard record view page as a pdf file and send it as attachment?

 

Thank you!!!

 

 

sfdcfoxsfdcfox
Using getContent() returns HTML, not a PDF, so of course it wouldn't work.

Using getContentAsPDF() should work... I just tried this, and I received an Internal Salesforce Error. Which means that it should work, except there's some bug with the platform.

I don't think there's any way you can fix this at the moment.