I have created a trigger on my invoice object when a update happen I want to send a email from my trigger with a pdf attachment. But when I try to update my record I am getting an error.

Error:Apex trigger TEST_on.sendEmailUpdate caused an unexpected exception, contact your administrator: TEST_on.sendEmailUpdate: execution of AfterUpdate caused by: System.VisualforceException: Getting content from within triggers is currently not supported.: Trigger.TEST_on.sendEmailUpdate: line 24, column 1

My trigger :

trigger sendEmailUpdate on Invoice__c (after update) {

public Invoice__c InvId = new Invoice__c (); 

if(trigger.isUpdate && trigger.isAfter){

    for(Invoice__c inv : trigger.New){
        if(InvName != null && InvName != '')
            InvName = InvName + inv.Name;
        else
            InvName = inv.Name;

    }

}
    //StreamingAPI

    PageReference pdf = Page.StreamingAPI;
    pdf.getParameters().put('id',(String)InvId.id);
    pdf.setRedirect(true);

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

    // Create the email attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('attachment.pdf');
    efa.setBody(b);

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {'emailID'};        
    mail.setToAddresses(toAddresses);
    mail.setTemplateId('00X90000001gwM8');        
    mail.setSubject('Your invoice been updated ' );        
    mail.setSubject('Your invoice been updated ' );
    mail.setPlainTextBody('Invoice Name ' + InvName );
    mail.setHtmlBody('Invoice Name ' + InvName);     
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });    
}

Please help me out why this error is accrue. If it is possible to send attachment using MassEmailMessage from this same trigger. Then please let me limitation with MassEmailMessage.

  • You will have to do this inorder to send in PDF attachments from triggers . Create a site link ( login disabled site) . Set the site Home page as teh PDF generating page . From the trigger , call in a batch and in the batch , use the SINGLE email messaging class to send PDF attachments . Just make sure you set the code this way : mail.setBody(give the site link here ). It will become an attachment by itself . By this way , you can send in PDF attachments using a trigger by an indirect way .Let me know if you need any help on the same.Thanks!! – TroubleShooter Oct 16 '14 at 7:48

Issue:

  1. You are using "PageReference" in trigger. Getting content from page in Trigger code is not allowed.
  2. Because this is trigger, Trigger is fire base on record insert/update/delete.
  3. Getting content is taking some time so when user update/create a record through UI it take more time to execute trigger.

Solution:

  1. You just need to create one future method into trigger with "callout" as "True".
  2. So, When trigger is fired, Salesforce is not waiting for that future method completion.

Updated your Code:

trigger sendEmailUpdate on Invoice__c (after update) {

public Invoice__c InvId = new Invoice__c ();

if(trigger.isUpdate && trigger.isAfter){

for(Invoice__c inv : trigger.New){
    if(InvName != null && InvName != '')
        InvName = InvName + inv.Name;
    else
        InvName = inv.Name;

}

}

//StreamingAPI
attachPDFInEmail(attachPDFInEmail);

}

@future(callout = true)
private static void attachPDFInEmail(String InvName){
    PageReference pdf = Page.StreamingAPI;
    pdf.getParameters().put('id',(String)InvId.id);
    pdf.setRedirect(true);

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

    // Create the email attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('attachment.pdf');
    efa.setBody(b);

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {'emailID'};        
    mail.setToAddresses(toAddresses);
    mail.setTemplateId('00X90000001gwM8');        
    mail.setSubject('Your invoice been updated ' );        
    mail.setSubject('Your invoice been updated ' );
    mail.setPlainTextBody('Invoice Name ' + InvName );
    mail.setHtmlBody('Invoice Name ' + InvName);     
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
}

use @future(callout=true) to generate pdf from visualforce

  • Can you expand on this? It doesn't really help on it's own with no explanation! – Matt Lacey Oct 26 '16 at 4:39

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.