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
DcoderDcoder 

Creating PDF attachments from custom object records

Hi All,

 

I am creating PDF attachments from custom object records. These PDFs are created from the VF page which is being rendered as PDF. This VF page shows the data from those records itself.

pdf = new PageReference('/apex/testVFpdf');
    
    
          pdf.getParameters().put('ID',<custom object record ID>);
          Blob pdfContent = pdf.getContent();
         
          temp_att = new attachment();
           
          temp_att.body = pdfContent;
          temp_att.ContentType = 'pdf';

          temp_att.

Name = 'test.pdf';

          temp_att.ParentId = <custom object record ID>;

          insert temp_att;

 

 

Now, the issue is , sometimes this attachment gets created correctly and sometimes not. Does anyone have any idea about it? How can the PDF file creates properly for most of the time and sometimes it is not able to open. When we try to open it in Adobe, it gives an error saying "either the file type is not supported or it got damaged due to incorrectly decoded". When I run my code again to create the same PDF file, it gets created correctly. So, the issue can not be in that record's content otherwise the PDF could not be created from it ever then.

 

Please share suggestions.

 

thanks!!

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DcoderDcoder

Hi All,

 

I managed a workaround to this issue. As we know that, getcontent() cannot be called from Batch Apex, triggers and scheduled apex. If we need to creat a PDF of the record when it is created, we can have a small VF section put on detail page of it and we can call the code to create PDF from one of functions in the VF page's extension and call that function from action event of that page, so it will be called each time that VF page is loaded. So , PDF will be created for that record then n there.

 

thanks

dcoder

All Answers

bob_buzzardbob_buzzard

Where does this code get executed from?  I.e. are you creating these through triggers, batch, scheduled apex or a combination?  And do you always execute it in the same way?

DcoderDcoder

Hi Bob,

 

I am doing it through scheduled Apex and I execute it in the same manner.

 

thanks

dcoder

DcoderDcoder

Hi All,

 

I managed a workaround to this issue. As we know that, getcontent() cannot be called from Batch Apex, triggers and scheduled apex. If we need to creat a PDF of the record when it is created, we can have a small VF section put on detail page of it and we can call the code to create PDF from one of functions in the VF page's extension and call that function from action event of that page, so it will be called each time that VF page is loaded. So , PDF will be created for that record then n there.

 

thanks

dcoder

This was selected as the best answer
tsalbtsalb

Long shot, but do you have the workign code you could paste?