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
Gaurav-GuleriaGaurav-Guleria 

System.VisualforceException: Getting content from within triggers is currently not supported.: Class.SendEmailToBillingContact.generateAndSendPDF: line 10, column 1

I am getting System.VisualforceException: Getting content from within triggers is currently not supported.: Class.SendEmailToBillingContact.generateAndSendPDF: line 10, column 1

Trigger Code:
trigger TriggerSendEmailToBillingContact on invoiceit_s__Invoice__c (after insert,after update) {
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();                   
    Map<ID,invoiceit_s__Invoice__c> Map_InvoiceitRec = new Map<ID,invoiceit_s__Invoice__c>([SELECT id,invoiceit_s__Account__r.invoiceit_s__Invoice_Delivery_Type__c,invoiceit_s__Invoice_Status__c,
                                 invoiceit_s__Billing_Contact__r.Email FROM invoiceit_s__Invoice__c WHERE Id IN :Trigger.New]);
    if(Trigger.IsInsert||Trigger.IsUpdate){
        for(invoiceit_s__Invoice__c invoiceRec:Map_InvoiceitRec.Values()){
            if((invoiceRec.invoiceit_s__Account__r.invoiceit_s__Invoice_Delivery_Type__c =='Email'||invoiceRec.invoiceit_s__Account__r.invoiceit_s__Invoice_Delivery_Type__c =='Email & Print') && invoiceRec.invoiceit_s__Invoice_Status__c=='Posted'){
                SendEmailToBillingContact.generateAndSendPDF(invoiceRec.Id);
                catch(Exception e){
                    Trigger.New[0].AddError('Error : '+e);
                   
                }                 
            }
        }
    }
}
Class Code:
public with Sharing class SendEmailToBillingContact {

    public static void generateAndSendPDF(Id invoiceRecId){
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();  
        // Reference the attachment page and pass in the Invoiceit ID
        PageReference pdf =  Page.InvoiceSummaryPDF;
        pdf.getParameters().put('id',(String)invoiceRecId);
        pdf.setRedirect(true);
        // Take the PDF content
        system.debug('ContentPDF:'+pdf.getContent());
        Blob b = pdf.getContent();
       
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setContentType('application/pdf');
        efa.setFileName('attachment.pdf');                 
        efa.Body = b;   ----------->Error at this Line
                           
        // Sets the paramaters of the email
        String[] toAddress = new String[]{'invoiceRec.invoiceit_s__Billing_Contact__r.Email'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddress);
        mail.setSubject( ' Your Martindale-Nolo Invoice' );
        mail.setHtmlBody('PFA');
        mail.setTemplateId('00Xc0000000DnyH');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });  
        emails.add(mail);
    }
}
Thanks in advance.
hitesh90hitesh90
Hello Gaurav,

You have to write Future method to call getContent method from trigger.
so set @Future in your controller.

see below sample code.
Apex Class:
public with Sharing class SendEmailToBillingContact {
    @Future
    public static void generateAndSendPDF(Id invoiceRecId){
              // your code is here
     }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
Gaurav-GuleriaGaurav-Guleria
Hi Hitesh,

Thanks for your reply.

adding @Future still gives this error.
hitesh90hitesh90
which error it has given?
can you please Post the error here. so i can get idea.
Gaurav-GuleriaGaurav-Guleria

Same error as given in the Subject.

hitesh90hitesh90
Have you used @Future keyword in your class?
Gaurav-GuleriaGaurav-Guleria
Yes, i have used:
public with Sharing class SendEmailToBillingContact {
    @Future
    public static void generateAndSendPDF(Id invoiceRecId){
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();  
        // Reference the attachment page and pass in the Invoiceit ID
        PageReference pdf =  Page.InvoiceSummaryPDF;
        pdf.getParameters().put('id',(String)invoiceRecId);
     
        pdf.setRedirect(true);
        // Take the PDF content
        system.debug('Invoice ID:'+invoiceRecId);
       
        Blob b = pdf.getContentasPDF();
       
       
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setContentType('application/pdf');
        efa.setFileName('attachment.pdf');                 
        efa.Body = b;
                           
       
        String[] toAddress = new String[]{'invoiceRec.invoiceit_s__Billing_Contact__r.Email'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddress);
        mail.setSubject( ' Your Martindale-Nolo Invoice' );
        mail.setHtmlBody('PFA');
        mail.setTemplateId('00Xc0000000DnyH');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });  
        emails.add(mail);
    }
}

hitesh90hitesh90
I have tried same scenario in my trigger it was worked. i think there is some other issue here.
kiran hebbarkiran hebbar
Hi,

I am facing simillar issue. It doesn't matter if you use @future or not. Same error occurs.