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
cedgcedg 

triggering apex method of extension controller

Hi,

 

i'm new in Trigger and Apex methods, so it's probably a dummy question.

 

I have an object SGRequest with a custom button to send PDF. (code found on the site of Jeff Douglas)

So i have a controller extension 

 

public with sharing class SGRequestPDFEmail{
 
   public SGRequest__c request {get;set;}

   public SGRequestPDFEmail(Apexpages.Standardcontroller con) {
      request = (SGRequest__c)con.getRecord();
   }
     
   public PageReference sendPdf() {
     
      PageReference pdf = Page.SGRequestPDFTemplate;
      // add parent id to the parameters for standardcontroller
      pdf.getParameters().put('id',request.id);
      // the contents of the attachment from the pdf
      Blob body;

      try {
         // returns the output of the page as a PDF
         body = pdf.getContent();
         // need to pass unit test -- current bug  
      } catch (VisualforceException e) {
         body = Blob.valueOf('Some Text');
      }
 
      Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
      attach.setContentType('application/pdf');
      attach.setFileName(request.File_Name__c);
      attach.setInline(false);
      attach.Body = body;
      
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setUseSignature(false);
      mail.setToAddresses(new String[] {request.email_author__c});
      mail.setSubject('PDF Email Demo');
      mail.setHtmlBody('Here is the email you requested! Check the attachment!');
      mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
 
      // Send the email
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+request.email_author__c));
 
      return null;
 
   }
 
}
This works fine, so when clicking on the button, the mail is correctly sent with the PDF in attachment.
Now i would like to trigger this after changing a field Status__c in my object SGRequest__c.
trigger SendPdfOnApproval on SGRequest__c (after update) {
    for (SGRequest__c sgr : Trigger.new){
        if ((sgr.Status__c != trigger.oldMap.get(sgr.id).Status__c) && (sgr.Status__c == 'Approved') ){
            sgr.sendPdf();
        }
    }
}
but of course, sgr doesn't know the method sendPdf ...
Can anybody help me ?
Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'll need to create it from your page/controller I'm afraid.

 

Again, from the docs regarding getcontent/getcontentaspdf

 

 

This method can't be used in:
• Triggers
• Scheduled Apex
• Batch jobs
• Test methods

 

All Answers

bob_buzzardbob_buzzard

I think you are out of luck on this one.  The method that you want to call pulls the contents of a PageReference using getContent().  According to the Apex Developer's Guide:

 

The getContent and getContentAsPDF PageReference methods aren't allowed in triggers.

cedgcedg

So it's not possible to send a PDF without an action button ?

bob_buzzardbob_buzzard

You'll need to create it from your page/controller I'm afraid.

 

Again, from the docs regarding getcontent/getcontentaspdf

 

 

This method can't be used in:
• Triggers
• Scheduled Apex
• Batch jobs
• Test methods

 

This was selected as the best answer
cedgcedg

ok, thanks for the info