0

I am working on converting some JS buttons to Lightning. These buttons invoke an Apex class that calls getContent() on a VisualForce page with a renderas attribute of PDF & then store that output in a QuoteDocument record. This logic works fine when I invoke it from a JS button. But when I try to invoke the same Apex class from a LWC Quick Action, the call to getContent() returns an HTML file w/a JS redirect to the VisualForce page.

I understand that getContent() is not allowed in Email Services & Triggers & Test classes. But is it also not allowed in LWC Quick Actions?

GetContent() seems to be behaving as if I'm not logged in. Is there something I need to do for getContent() to run as if I'm logged in?

Do I need to replace my call to getContent with an HTTP request to the VisualForce page (which would require setting up a Named Credential for it)?

Here is the Apex class in question:

global class QuoteDocumentFactory_EU {
    @AuraEnabled
    Webservice static string createPdf(string quoteId) {
        Quote quote = [select CurrencyIsoCode, Discount, GrandTotal from Quote where Id = :quoteId];
        PageReference pdf = new PageReference('/apex/QuotePDF_EU?id=' + quoteId);        
        Blob content = (!Test.isRunningTest()) ? pdf.getContent() : blob.valueof('TEST');
        QuoteDocument document = new QuoteDocument(
            QuoteId = quoteId,
            Document = content
            );
        insert document;
        return document.Id;        
    }   
}

1 Answer 1

0

Salesforce support solved the issue for me. It turns out the issue was that I shouldn't have used a relative path of the Visualforce page.

This is the code change they made.

global class QuoteDocumentFactory_EU {
    @AuraEnabled
    Webservice static string createPdf(string quoteId) {
    Quote quote = [select CurrencyIsoCode, Discount, GrandTotal from Quote where Id = :quoteId];
    String vfHostname = DomainCreator.getVisualforceHostname('c'); //Get the Visualforce hostname
    String vfPageUrl = 'https://' + vfHostname + '/apex/QuotePDF_EU?id=' + quoteId; //Build the URL
    PageReference pdf = new PageReference(vfPageUrl);
        Blob content = (!Test.isRunningTest()) ? pdf.getContent() : blob.valueof('TEST');
        QuoteDocument document = new QuoteDocument(
            QuoteId = quoteId,
            Document = content
            );
        insert document;
        return document.Id;        
    }   
}

You must log in to answer this question.

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