You need to sign in to do that
Don't have an account?
pdf attachment by trigger '%PDF-' error
Hi,
*****************************************************************************************************************************************************************************************
Background:
I have created a custom object called Invoice, and all my invoices have a master details relation with the Opportunity object.
When an invoices is created i set a trigger to auto attach my pdf to the invoice's opportunity .
I use the same code as i use for my quote (except that for the quote salesman are clicking to attache the pdf) here i need to make it automatic .
*****************************************************************************************************************************************************************************************
So far it's looks like working except that i'm not able to open my pdf ,i got the following error :
file does not start by '%PDF-'
Here my code :
Trigger:
trigger AttachInvoices on Invoice__c (after insert) { for(Invoice__c inv : Trigger.new){ //TestAttachInvoices.savePdf(inv.Id); TestAttachInvoices.savePdf(inv); } }
Class:
public class TestAttachInvoices { public static void savePdf(Invoice__c inv){ Pagereference pdfPage = Page.InvoiceDeliveryA; pdfPage.getParameters().put('Id',inv.id); //the contents of the attachment from my pdf Blob pdfBlob; try{ pdfBlob = pdfPage.getContent(); }catch(VisualforceException e){ pdfBlob = Blob.valueOf('error'); } Attachment attach = new Attachment(parentId = inv.id, name= inv.Name + '.pdf', body = pdfBlob); attach.IsPrivate = false; try{ insert attach; } catch(DmlException d){ d.getMessage(); } } }
does anyone can help me ? =(
Hi,
I think it is stated in the documentation that getContent or getContentasPDF does not work in trigger.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm
I just knew. Thanks to you.
Regards,
Hengky
All Answers
Try getContentAsPDF() instead of getContent().
Hengky
It doesn't help = /
If you save the file, and open the file using a text editor, what do you see?
Regards,
Hengky
error
Your code must have caught an exception:
Maybe you can output more detail of your exception to the pdfBlob to troubleshoot the error instead of just plain "error".
Regards,
Hengky
I think it's this:
17:03:03:188 EXCEPTION_THROWN [21]|System.VisualforceException: Getting content from within triggers is currently not supported.
i tried to give a look but i can't find anything...:/
Hi,
I think it is stated in the documentation that getContent or getContentasPDF does not work in trigger.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm
I just knew. Thanks to you.
Regards,
Hengky
no wonder i can't do it!
Thanks for your help!