Oct 14, 2024
88 Views
Comments Off on System.VisualforceException: Getting content from within triggers is currently not supported
0 0

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

Written by

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 <String> setOfContactIds = new Set <String>(); 
    for (Contact objCon: trigger.new) {
        setOfContactIds.add(objCon.id); 
    }
    if (setOfContactIds.size() > 0) {
        ContactTriggerHelper.generatePdf(setOfContactIds); 
    }
}

ContactTriggerHelper:

public class ContactTriggerHelper { 
    @future(callout = true)
    public static void generatePdf(Set <String> setOfIds) {
        List <Attachment> listOfAttach = new List <Attachment>();
        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() > 0)
        { 
            insert listOfAttach;
        }
    }
}

Thanks

Article Categories:
Salesforce
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.