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
AntonyasLenAntonyasLen 

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 ? =(

Best Answer chosen by Admin (Salesforce Developers) 
Hengky IlawanHengky Ilawan

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

Hengky IlawanHengky Ilawan

Try getContentAsPDF() instead of getContent().

 

Hengky

AntonyasLenAntonyasLen

It doesn't help = /

Hengky IlawanHengky Ilawan

If you save the file, and open the file using a text editor, what do you see?

 

Regards,

Hengky

AntonyasLenAntonyasLen

error

Hengky IlawanHengky Ilawan

Your code must have caught an exception:

 

...
}catch(VisualforceException e){
   pdfBlob = Blob.valueOf('error');
}
...

Maybe you can output more detail of your exception to the pdfBlob to troubleshoot the error instead of just plain "error".

 

Regards,

Hengky

AntonyasLenAntonyasLen

I think it's this:

 

17:03:03:188 EXCEPTION_THROWN [21]|System.VisualforceException: Getting content from within triggers is currently not supported.

AntonyasLenAntonyasLen

i tried to give a look but i can't find anything...:/

Hengky IlawanHengky Ilawan

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

This was selected as the best answer
AntonyasLenAntonyasLen

no wonder i can't do it!

Thanks for your help!