0

I am trying to automate the generation of quote pdfs. I have found a really nice blog entry on that topic here: https://automationchampion.com/2020/06/23/getting-started-with-process-builder-part-97-now-generating-a-quote-pdf-is-a-breeze-learn-how/?fbclid=IwAR0QHkSOjk2TG9hzdthNAbJ88hQjKjGX8FuV_UicCcnT_N-sfp5PJBicXVo

Everything works fine except for the fact that pdf just turns out completely blank.

Here the code from github:

Public class generateQuotePdfDocument{
@InvocableMethod   

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

public static void LeadAssign(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=10';
        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);
}
}
}

Any thoughts what might be the problem?

3
  • is Label.QuoteTemplateId pointing at a real Quote Template?
    – cropredy
    Commented Jul 13, 2020 at 19:01
  • Hey, yes I am pretty sure. It links to a label whos value is '0EH2o000000lqbA'. Although the only way I obtained this ID is via the url of the template in the edit mode. Did not find any other way to access a template ID. I found some comments that one would need a combination of invocable and future method here for reasons I did not really understand. I tried to rewrite my code so that the process builder would trigger the @future method but with no luck. No pdf at all being created.
    – Jan Tonnet
    Commented Jul 14, 2020 at 5:17
  • hmm -- i ran the code in my org and got a PDF -- try running in execute anonymous the code within the method using a hardcoded quoteId and see if that works. As for async, I assume you are referring to this answer - in your automation, does the quote already exist as a result of a PREVIOUS transaction or is quote created as part of same transaction as the invocable method?
    – cropredy
    Commented Jul 14, 2020 at 17:53

1 Answer 1

0

I was able to fix this for me using @future(callout=true)

Public class generateQuotePdfDocument{

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

@InvocableMethod   
public static void QuoteCreate(List<Id> quoteIds)  {
 callFutureMethod(quoteIds);
}

@future(callout=true)

public static void callFutureMethod (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=10';
        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);
}

}
}

You must log in to answer this question.

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