You need to sign in to do that
Don't have an account?
Gaurav-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.
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.
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/
Thanks for your reply.
adding @Future still gives this error.
can you please Post the error here. so i can get idea.
Same error as given in the Subject.
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);
}
}
I am facing simillar issue. It doesn't matter if you use @future or not. Same error occurs.