function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Janno RipJanno Rip 

Call Apex Class from Trigger after Update on Object

Hello everyone,

I guess this is as standard as it gets but I still struggle as newby in apex classes and triggers.

I have an apex class that - as for now - is being invoked by a process builder. But in regards of performance I want to switch to an apex trigger.

Here is the class:

Public class generateQuotePdfDocumentByTrigger{

//https://github.com/Rakeshistom/Auto-Generates-Quote-PDF

public static void QuoteCreate (List<Id> quoteIds) {

    //Initialize the quote url
    String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?';
    
    //Get the Quote Template Id from Custom Settings
    String quoteTemplateId = Label.QuoteTemplateId;
    
    //List variable to get all the Quote Documents
    List<QuoteDocument> lstQuoteDoc = new List<QuoteDocument>();
    
    if(!quoteIds.isEmpty() && quoteIds.size() > 0) {
        
        for(Id quoteId :quoteIds) {
            //Construct the quote URL to generate PDF
            quoteUrl += 'id=' + quoteId;
            quoteUrl += '&headerHeight=197&footerHeight=80';
            quoteUrl += '&summlid=' + quoteTemplateId;
            
            //call the quote url
            PageReference pageRef = new PageReference(quoteUrl);
            
            //get the quotePdf
            Blob quoteBlob;
            
            if(Test.isRunningTest()) {
                quoteBlob = Blob.valueOf('Generate Pdf');
            } else {
                quoteBlob = pageRef.getContentAsPDF();
            }
            
            //initialze the QuoteDocument to hold the quote pdf for insertion
            QuoteDocument quoteDoc = new QuoteDocument();
            quoteDoc.Document = quoteBlob;
            quoteDoc.QuoteId = quoteId;
            lstQuoteDoc.add(quoteDoc);
        }
    }
    
    if(!lstQuoteDoc.isEmpty() && lstQuoteDoc.size() > 0) {
        Database.insert(lstQuoteDoc);
    }

}
}
What I like the trigger to do is the following:

After "Create_PDF__c" is set to "true" - invoke the apex class.

This is what I got so for but yeah...

Error: Compile Error: Method does not exist or incorrect signature: void generateQuotePdfDocumentByTrigger() from the type CreatePDFQuote_JTo at line 7 column 9
trigger CreatePDFQuote_JTo on Quote (after update) 

{
    for(Quote currentQuote : Trigger.New)
    {
        if(currentQuote.Create_PDF__c == true ) {
        generateQuotePdfDocumentByTrigger();
        }
    }
    
  
}

Thanks in Advance!
David Zhu 🔥David Zhu 🔥
You may use the code below.
trigger CreatePDFQuote_JTo on Quote (after update) 
{
    List<Id> quoteIds = new List<Id>();

    for(Quote currentQuote : Trigger.New)
    {
        if(currentQuote.Create_PDF__c) 
        {
           quoteIds.add(currentQuote.Id);
        }
    }
    
    if (quoteIds.size() > 0)
    {
        generateQuotePdfDocumentByTrigger.QuoteCreate (quoteIds);
    }
  
}
Janno RipJanno Rip
Thank you for your reply @David Zhu,

unfortunately I am running in the following error:

The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: CreatePDFQuote_JTo: execution of AfterUpdate caused by: System.VisualforceException: Getting content from within triggers is currently not supported. Class.generateQuotePdfDocumentByTrigger.QuoteCreate: line 33, column 1 Trigger.CreatePDFQuote_JTo: line 15, column 1.
Janno RipJanno Rip
Nvm. I figured out that a @Future would do the trick:
 
@InvocableMethod   
public static void QuoteCreate(List<Id> quoteIds)  {
  callFutureMethod(quoteIds);
}

@future(callout=true)

public static void callFutureMethod (List<Id> quoteIds) {
...
}
It is now working and the class does what it should meaning creating PDFs. However I am creating 3 PDFs now at once for 1 quote. Is this due to (before update) ? 
 
David Zhu 🔥David Zhu 🔥
It should be in after Update trigger. from the code, all pdfs should be created at the same time but should be associated to different quotes.