+ Start a Discussion
akhil Vakhil V 

Pdf Attachment Error

Hi,
Here is the problem i have written a trigger that inserts a record along with that vf created pdf.when i execute from console the reocrd and pdf is inserting when i click on pdf content is exact but when i insert a record manually,pdf is also created but when i download the pdf file it is getting null pdf?please Help out.
 if(!Test.isRunningTest()) 
                    {
                        b = pdf.getContent();
                        
                        System.debug('blobcontent+++++++++++'+b );
                    }
                    else
                    {
                        b = Blob.valueof('Some random String');
                    }
                    
                    n = new Attachment();
                    n.ParentId = id;
                    n.Name = 'Carrier_Confirmation.pdf';
                    
                    n.Body = pdf.getContentAsPDF();
                  
                        //n.Body = Blob.valueof('Some random String');
                 
                    n.IsPrivate = false;
                    n.contentType = 'image/pdf';
                    
                    insert n;
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi Akhil,

In simple, you cannot create pdf from a trigger. getContentAsPDF() cannot be used in Triggers, Schedule and batch apex..

Please refer this : https://www.salesforce.com/docs/developer/pages/Content/apex_System_PageReference_getContentAsPDF.htm

Thanks,
Thiyagarajan Selvaraj.
akhil Vakhil V
Hi Thiyagarajan Selvaraj,
Thanks for the Reply!
How to acheive this?Here i have a more requirement in that i have 3 vf pdf doc. I need to add 3 pdf's at the time of insertion of record using trigger?
Any help will be Aprreciable!!
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi Akhil,

You can call webservice from a trigger and add the attchment creation logic in the webservice.

Refer this : https://jungleeforce.wordpress.com/2014/06/11/generate-a-pdf-and-attach-it-to-record-from-a-trigger-in-salesforce-restful/
 
akhil Vakhil V
Hi Thiyagarajan Selvaraj,

Is their any way without writing the webServices?
Thanks,
Akhil.