Salesforce | System.VisualforceException: Getting content from within triggers not supported

Author - Webner

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});
}
}

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

One response on “Salesforce | System.VisualforceException: Getting content from within triggers not supported

  1. […] is online discussion about the error, and a solution – move the getContent() call into a future method, and then call that from within the class […]

Leave a Reply

Your email address will not be published. Required fields are marked *