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
DuFreyDuFrey 

Saving renderAs PDFs as attachements

I am generating a quote and rendering it as a PDF and also attempting to save it as an attachment in the quote with the following code:

Code:
public PageReference SaveContent() {   
     PageReference pdfPage = Page.QuotePDFPage;
     pdfPage.getParameters().put('quoteid',quoteId);
     pdfPage.getParameters().put('oppid',OppId);
pdfPage.getParameters().put('ownerid',OwnerId);
Blob pdfBlob = pdfPage.getContent();
Attachment a = new Attachment(ParentId = quoteId, Name = 'Quote.pdf', ContentType = 'pdf', Body = pdfBlob);
insert a;
return null;
}

When I generate a quote, an attachment is created but when I try to open it. I receive the error "Attempt to de-reference a null object".

I believe this is from not having all the correct object ids being passed. For the quotes when generated, I am passing the quote id, opportunity id, and user id.

I have tried several ways to attempt to pass the two additional ids using getParameters(), but none seem to work. How can I also pass these to the attachment? Do I also need to call the action from a different apex page then the one generating the quote?

Ron HessRon Hess
i think you will have to put debug statements inside the QuotePDFPage page constructor and see if those variables are set properly.

you can do this with the system log window, but if you don't see your messages, create a debug log using setup, adminstration, monitoring ->debug log

then run your code and look in the logs generated to see the ids's are passed correctly.


if the id's are not , you can try this method

PageReference pdfPage = new PageReference('/apex/QuotePDFPage?quoteid=xxx&oppid=YYY....');

those variables should be visible in as parameters in the new page

verify that you can generate the pdf page by redirecting to it , then once that is working, generate the attachment
mtbclimbermtbclimber
Can you please clarify this:


DuFrey wrote:

When I generate a quote, an attachment is created but when I try to open it. I receive the error "Attempt to de-reference a null object".



Is this what you see in the PDF?

It would help if you could post a complete page (including the quotePDFPage) + controller that exhibit the behavior. Makes it easier for those who want to help you :smileyhappy:

DuFreyDuFrey
Andrew - Yes I received the de-reference message inside the PDF attachment itself instead of the quote info.

Ron - Thank you for those suggestions. That worked. By using a new page reference,  was able to pass in all the required ids before saving it as an attachment.

Thanks again,
Fred


beenerbeener
Would you please share the new - Working code?

Thanks so much.

Ben
DuFreyDuFrey
Code:
PageReference pdfPage = new PageReference('/apex/'+pageName+'?quoteid='+quoteId...);
Blob pdfBlob = pdfPage.getContent();
Attachment a = new Attachment(ParentId = quoteId, Name = 'Quote.pdf', ContentType = 'pdf', Body = pdfBlob);
insert a;

 I used code similar to the above to generate and save an attachment as a PDF.