2

I created the following to create Quote records and PDFs for all of them. The code runs up until QuotePDF.Document = QuotePage.getContentAsPDF();and then just stops, with no errors of any kind. The quote records which were already inserted do not actually show up in SF. Never seen such an empty failure with no message before. Any ideas?

public class EmailPDFQuote implements Queueable, Database.AllowsCallouts {

    public List<Opportunity> Opps;
         
    public void execute(QueueableContext context) {
        system.debug('Opps = '+Opps);
        Quote [] QuotesToInsert = new List<Quote>();
        
        for(Opportunity Opp: Opps){
            
            Quote Q = new Quote(Name = Opp.Name, OpportunityID = Opp.ID);
                QuotesToInsert.add(Q);
        }
        system.debug('QuotesToInsert = '+QuotesToInsert);
        try{
            insert QuotesToInsert;
            system.debug('QuotesToInsert = '+QuotesToInsert[0].Id);
        }
        catch(DMLexception e){
            system.debug(e);
        }

        
        QuoteDocument [] PDFs = new List<QuoteDocument>();
            system.debug('QuotesToInsert = '+QuotesToInsert);        
        for(Quote Q: QuotesToInsert){
            PageReference QuotePage = new PageReference('/quote/quoteTemplateDataViewer.apexp?id='+Q.Id+'&summlid=0EH1Y000000zeer'); //quoteUrl +='&headerHeight=190&footerHeight=188&#toolbar=1&navpanes=0&zoom=90'; 
                system.debug('QuotePage = '+QuotePage);
            QuoteDocument QuotePDF = new QuoteDocument(); 
                system.debug(QuotePDF);
                QuotePDF.Document = QuotePage.getContentAsPDF();  //Blob b = pr.getContentAsPDF();  
                QuotePDF.QuoteId = Q.Id;  
            system.debug(QuotePDF);
            PDFs.add(QuotePDF);
                system.debug(QuotePDF);
        }
            system.debug('PDFs = '+PDFs);
        try{
            insert PDFs;
                system.debug('ok');
        }
        catch(DMLexception e){
            system.debug(e);
        }                 
    }
}
1
  • I broke this into two separate jobs and it worked, but still no idea why it didn't work here.
    – number41
    Commented Feb 21, 2021 at 8:10

1 Answer 1

4

The getContentAsPDF call works in a separate transaction, and as the transaction it is called from has not yet committed, the data is not visible. Hence the need to break this up into two sequential transactions. See e.g. Isolation (database systems).

It is a pity this isn't clearly called out in the documentation; instead it is implied via this:

getContentAsPDF is treated as a callout in API version 34.0 and later

1
  • Thanks - they also ought to create some kind of message in the logs instead of making it look like SF is just broken!
    – number41
    Commented Feb 22, 2021 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .