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
rmillrmill 

Generate PDF with apex trigger

I am currently trying to have a PDF created based on a field update.

The issue is that it seems that getContent() and getContentAsPDF() don't work when called within a trigger. 

 

Does anyone know a work around for this or is this something that I won't be able to accomplish.\

 

Thanks

Alex.AcostaAlex.Acosta

Have you tried seperating this logic into a class with @future, and then calling it to do your logic asynchronously?

rmillrmill

It now allows me to do it except the only problem is that the PDF is now coming out blank.

 

global class pdfCreator
{
    @future (callout=true)
    public static void createPDF(ID Id)
    {
        PageReference pdf = Page.AccountsPDF;
        Attachment attach = new Attachment();
        attach.ParentId = Id;
        attach.name = 'name.pdf';
        attach.body = pdf.getContentAsPDF();
        insert attach;
    }
}

 

trigger CreatePDF on Contract_Custom__c (after update) {

    for(Contract_Custom__c a : Trigger.New)
    {
    	Id id = a.id;
    	pdfCreator.createPDF(Id);
    }
}

 above is the code for my class and trigger

Alex.AcostaAlex.Acosta

You probably need to pass in the ID otherwise the page doesn't know what it's rendering....

 

try the following if your page has a standardController for Account:

@future (callout=true)
public static void createPDF(ID Id)
{
        PageReference pdf = Page.AccountsPDF;
        pdf.getParameters().put('id', String.valueOf(Id));

        Attachment attach = new Attachment();
        attach.ParentId = Id;
        attach.name = 'name.pdf';
        attach.body = pdf.getContentAsPDF();
        insert attach;
}

 

rmillrmill

I tried the fix above but unfortunately it is still returning a blank pdf.

Alex.AcostaAlex.Acosta

Looking back at old code, when I had to do something similar... only difference I can really tell is the SetRedirect Option... This was the code I used....

 

private Messaging.EmailFileAttachment generatePDFAttachment(Invoice_Object__c i){				
	// Reference the attachment page and pass in the Id
	PageReference pdf = Page.attachmentPDF;
	pdf.getParameters().put('id',(String)i.id); 
	pdf.setRedirect(true);

	// Take the PDF content
	Blob b = pdf.getContent();

	// Create the email attachment  
	Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();        
	efa.setFileName(i.Invoice_Due_Date__c.format() + '.pdf');
	efa.setBody(b);
	
	return efa;
}

 

Visualforce Page

<apex:page standardController="Invoice_Object__c" renderAs="PDF">
        <c:attachment objectInstance="{!Invoice_Object__c}"/>
</apex:page>

 

Component was just used mostly to style the page

<apex:component >
        <apex:attribute name="objectInstance"  type="Invoice_Object__c"
      required="true" />
        <!-- Table Container -->
        <table style="width: 100%">
...
        </table>
</apex:component>

 

dimetrius.bydimetrius.by

Hello, 

 

The issue is that you are not able to use getContent() or getContentAsPDF() methods in:

 

  • Triggers
  • Scheduled Apex
  • Batch jobs
  • Test methods
  • Apex email services

Please see another way http://corycowgill.blogspot.ch/2012/02/generating-pdf-in-apex-trigger.html

 

Hope it helps.

 

Alex.AcostaAlex.Acosta

Does this include asynchronous calls though? I thought this was possible through the use of method with @future annotation.

dimetrius.bydimetrius.by

Hi Alex,
 Unfortunately the getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

 

Thanks

Ganesh HegdeGanesh Hegde
Now this can be achived using Process Builder. 
AngrySlothAngrySloth
Ganesh could provide an example of this for the Process Builder.  I am trying to get this to work but keep getting the folowing error:
 
caused by: Record Currently Unavailable: The record you are attempting to edit, or one of its related records, is currently being modified by another user. Please try again


even though no fields are changing on the record.
 
East KenEast Ken
Here's another way of create PDF (http://www.rasteredge.com/how-to/csharp-imaging/pdf-creating/), hope it will help you.