You need to sign in to do that
Don't have an account?
DuFrey
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:
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?
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?
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
DuFrey wrote:
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:
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
Thanks so much.
Ben
I used code similar to the above to generate and save an attachment as a PDF.