0

I have a Batch class that calls a Visualforce page using getContentAsPDF() to generate a single PDF. This works flawlessly in a scratch org and in the developer logs I can see a separate call made to the Visualforce page as expected.

In production however, the logs show that the batch runs as expected, records are fetched and processed but a blank file is generated. There are no errors in the production logs and the call to the Visualforce page is never made.

To illustrate and simplify the code I've created another batch class as below and run it against the scratch org and production which yielded the same results:

public class BatchTestPDF implements Database.Batchable<sObject>, Database.AllowsCallouts {

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT ID FROM Contact WHERE Lead_Score__c = 9999');
    }

    public void execute(Database.BatchableContext bc, List<Contact> contacts){
        // Generate PDF
        PageReference pageRef = new PageReference('/apex/TestPDF');
        Blob contentBlob = pageRef.getContentAsPDF(); 
        // Save file to contact record
        ContentVersion cv = new ContentVersion();
        cv.ContentLocation = 'S';
        cv.Title = 'Test File';
        cv.PathOnClient = 'test.pdf';
        cv.VersionData = contentBlob;
        insert cv;
        cv = [SELECT ContentDocumentId FROM ContentVersion WHERE ID = :cv.Id];
        ContentDocumentLink contentDocumentLink = new ContentDocumentLink(
            LinkedEntityId = contacts[0].ID,
            ContentDocumentId = cv.ContentDocumentId,
            ShareType = 'I'
        );
        insert contentDocumentLink;
    }

    public void finish(Database.BatchableContext bc){
    }

}

TestPDF.page

<apex:page renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="false">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        </head>
        <body>
            <p>This is a test</p>
        </body>
    </html>
</apex:page>

Am I missing a permission or some other setting in production for this to work?

2
  • Could it be that the running user is missing access to the resources in prod? Check if "BatchTestPDF" is added under "Apex Class Access", and "TestPDF" is added under "Visualforce Page Access" in profile or permission sets for the user that triggered the batch. Commented Aug 9, 2023 at 16:10
  • Yeah, permissions on the my profile have been set for both and I'm guessing when calling the batch from the developer console it would either run using my permissions or as system user, so either way that should be fine. Thanks
    – Dan
    Commented Aug 9, 2023 at 22:51

1 Answer 1

2

Salesforce support was able to help me by pointing out that my profile had "Session Security Level Required at Login" set to High Assurance which forces MFA. High Assurance blocks processing of getContentAsPDF within batch jobs.

There are other ways to require MFA at login including setting "Require multi-factor authentication (MFA) for all direct UI logins to your Salesforce org" under Identity Provider in Setup.

The following articles provide more detail:

https://help.salesforce.com/s/articleView?id=000392426&type=1

https://issues.salesforce.com/issue/a028c00000qPxndAAC/two-factor-authenticated-user-having-session-setting-set-as-high-assurance-and-async-apex-generated-pdf-attachment-file-content-shows-as-blank

2
  • Can you please reproduce the important parts of those links here? Salesforce doesn't have a great reputation when it comes to link longevity. Including the important parts of the links here will mean that this answer can still help people after Salesforce breaks their own links again (not a matter of if, but a matter of when)
    – Derek F
    Commented Sep 12, 2023 at 15:45
  • 1000 thanks for this, I was facing the exact same issue, I lost so much time on this before finding the answer here, way better than ChatGPT !!!
    – Chris
    Commented May 7 at 10:37

You must log in to answer this question.

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