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
Liron CohenLiron Cohen 

Open PDF & save it as attachment

Hi.
I want to generate PDF document & save it as attachment under the related record (at the same function).
Is that mean I need to generate the PDF twice?

e.g.:

public Pagereference openAndAttach(){
PageReference documentPage = new PageReference('apex/myDoc?id='+recId);

//Create Attachment
Blob b = documentPage.getContect();
Attachment attach = new Attachment();   
attach.Body = b;
attach.ParentId = recId;
insert attach;

//Open PDF
return documentPage;
}


Is there a better way for doing this? It seems like wasting as the code generating the PDF.