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
MJ09MJ09 

Calling getContent() from a trigger

Three days after a particular condition is met, I need to send an email message that includes a PDF attachment. I've set up workflow to do a field update when the time-based condition is met. I have a trigger that detects the field update and generates the email message.

 

To generate the PDF attachment, the trigger has to get a PageReference to the VF page, and then call either getContent() or getContentAsPDF(). Unfortunately, these methods aren't supported in a trigger.

 

[Actually, the documentation says that getContent() isn't supported in a trigger, but it doesn't say the same thing about getContentAsPDF(). It makes sense that, if getContent() isn't supported, neither is getContentAsPDF(), but the documentation should say so. But I digress.]

 

Since the documentation doesn't say anything about getContent() not being supported with @future methods, I wrote an @future method that generates the PDF and sends the email, and changed my trigger to call that. But apparently getContent() doesn't work from an @future method. 

 

[Interestingly, getContent() fails differently when called from a trigger than it does when called from @future. When called from a trigger, getContent() throws an exception. When called from an @future method, getContent() doesn't fail -- it just generates a blank PDF, which is the same the thing doc says it does when called from a unit test. But again, I digress.]

 

Because of the time-based nature of the condition that causes the email to be sent, I need to use workflow to detect the condition. The PDF attachment is complex enough that it needs its own VF controller, so I can't use a messaging:attachment, and I can't use a workflow email action. I need to use a trigger to send the email. Any suggestions on how I can get a trigger to construct and send this email message with its attachment?

 

mcpluscombemcpluscombe

I have managed to get a work around by using a http req call to a sites page that creates the PDF, attaches it to the object and emails it to the current user, by doing the getcontent call from the controller.  You just need to pass the parameters you want to the sites page. 

 

This is an example of the call to a sites page passing 2 parameters, the ID of the object and the current users ID.  Don't forget to make sure you add your sites domain to the Remote Access list and that your sites guest user has access to the fields and objects that it is creating the PDF from

 

The Trigger

 

 

trigger send_pdf_job on Opportunity (after update) {    
  
   for (Opportunity opp: Trigger.New)
   {
   
    ID user_ID = UserInfo.getUserId();
    ID LineID = opp.Id;
    CreatePDF.sendjob(LineID, user_ID);
    
    }

}

 

The class

 

 

global class CreatePDF {
 
    @future (callout=true)
    
    Public static void sendjob(ID LineID, ID user_ID){
   
    
     HttpRequest req = new HttpRequest();
   
//set to your sites page that handles the PDF page creation
     req.setEndpoint('your_sites_page');
//
     req.setBody('id='+LineID+'&user='+user_ID);
     req.setMethod('POST');
     
     
     Http http = new Http();
     try{
         HTTPResponse res = http.send(req);
     }catch(Exception e){
         
     }
   }
}
JaromirJaromir

but how did you attach it since there is no getContent on HttpResponse. Simply casting the result.getBody() to a blob doesn't work. I get an empty pdf.. Any ideas?

viv5586viv5586

Please post your work around for this if you found one as I am facing the same issue currenlty

JitendraJitendra
Its already too late to answer, but for Others coming to this Question, Please check this blog article. It resolves problem.

http://www.shivasoft.in/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/

Trigger -> Asynch Method -> REST based Apex class -> Send Email

Thanks,
Jitendra Zaa