We have a scenario where we have to add dynamic content in attachments from apex. On click of a button, I insert a list of Contacts. And in the next line, I pass it's id as parameter for a visualforce page which is rendered as PDF and used for the attachment. So this is done for each Contact in the list. But, when the .getContent() method is called, I get a visualforce exception : Unable to retrieve object. The same code works when called from a button on the record detail page. But calling this just after performing insert gives me this error.
/* code snippet */
insert lstContacts;
for(Contact c : lstContacts)
{
sendEmail(c.Id);
}
...
...
private void sendEmail(String recordId)
{
Pagereference pdf = Page.AttachmentPage;
pdf.getParameters().put('id',recordId);
pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContent(); // HERE I GET THE EXCEPTION
}
It is a StandardController page and works fine for records already existing. Is there a limitation for not being able to access a record's standard controller right after inserting it? Need some help here.