SystemVisualforceException - Getting content from within triggers is currently not supported

System.VisualforceException: Getting content from within triggers is currently not supported

System.VisualforceException: Getting content from within triggers is currently not supported
(Salesforce Visualforce Error Notification)
—————————-

Hi All,
Recently I was working on a task in which we need to save a pdf page as an attachment when a new record(Contact) is inserted.

I was doing it with Trigger and its handler using Pag reference Method getContentAsPdf(). I got an exception when I was saving a record of contact. This problem will be resolved if we use @future(callout=true) annotation before the method

Sample Pdf page:


< apex: page renderAs = ”pdf” >

 < !–Begin Default Content REMOVE THIS– >

 < h1 > Hello < /h1>

This is your pdf page

 < !–End Default Content REMOVE THIS– >

 < /apex:page> ContactTrigger:

trigger ContactTrigger on Contact(After Insert) {
 Set & lt;
 String & gt;
 setOfContactIds = new Set & lt;
 String & gt;
 ();
 for (Contact objCon: trigger.new) {
  setOfContactIds.add(objCon.id);
 }
 if (setOfContactIds.size() & gt; 0) {
  ContactTriggerHelper.generatePdf(setOfContactIds);
 }
} < strong > ContactTriggerHelper < /strong>
public class ContactTriggerHelper {
 @future(callout = true)
 public static void generatePdf(Set & lt; String & gt; setOfIds) {
  List & lt;
  Attachment & gt;
  listOfAttach = new List & lt;
  Attachment & gt;
  ();
  for (String conId: setOfContactIds) {
   PageReference pdf = Page.ContactInPdf;
   Attachment attach = new Attachment();
   attach.ParentId = conId;
   attach.name = conId + '.pdf';
   attach.body = pdf.getContentAsPDF();
   listOfAttach.add(attach);
  }
  if (listOfAttach.size() & gt; 0) {
   insert listOfAttach;
  }
 }
}

Thanks

0 Comments

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2020 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?