Problem: System.VisualforceException: Getting content from within triggers is currently not supported.
Description: This Visualforce exception occurs when we use getContent method in the trigger of sobject.
Here is the code for which we got this exception:
Trigger code:
trigger updateStatus on sobject (before update) { List List = new List(); for(sobject term : Trigger.new){ sobject oldOpp = Trigger.oldMap.get(term.Id); if(term.Payment_Status__c =='Paid'){ Messaging.SingleEmailMessage emailTobeSent = new Messaging.SingleEmailMessage(); PageReference pdf = Page.templateCOI; //Replace attachmentPDF with the page you have rendered as PDF pdf.getParameters().put('Id',term.Id); pdf.setRedirect(true); Attachment attach = new Attachment(); Blob b; b = pdf.getContent();// Exception: Getting content from within triggers is currently not supported attach.Body = b; attach.Name = 'name'; attach.IsPrivate = false; attach.ParentId = term.Id; insert attach; Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('Confirmation of Insurance.pdf'); efa.setBody(b); List listEmailMembers = new List(); listEmailMembers.add(Email); emailTobeSent.setToAddresses(listEmailMembers); emailTobeSent.setSubject('Subject'); emailTobeSent.setHtmlBody('body'); emailTobeSent.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailTobeSent}); } } }
Solution: To resolve this exception, we need to use getContent method in apex class with future method.
Here is the code for this:
Trigger code: trigger updateStatus on sobject (before update) { List List = new List(); for(sobject term : Trigger.new){ sobject oldOpp = Trigger.oldMap.get(term.Id); if(term.Payment_Status__c =='Paid'){ sendPDFEmail.sendPDF(term.Id,term.Email__c); } } }
Apex Class code:
Public class sendPDFEmail{ @future public static void sendPDF(String Id,String Email){ Messaging.SingleEmailMessage emailTobeSent = new Messaging.SingleEmailMessage(); PageReference pdf = Page.templateCOI; pdf.getParameters().put('Id',Id); pdf.setRedirect(true); Attachment attach = new Attachment(); Blob b ; b = pdf.getContent(); attach.Body = b; attach.Name = 'name'; attach.IsPrivate = false; attach.ParentId = Id; insert attach; Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('Confirmation of Insurance.pdf'); efa.setBody(b); List listEmailMembers = new List(); listEmailMembers.add(Email); emailTobeSent.setToAddresses(listEmailMembers); emailTobeSent.setSubject(‘subject’'); emailTobeSent.setHtmlBody('body'); emailTobeSent.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailTobeSent}); } }
Leave A Comment