I am trying to send an email while using PageReference and the getContent() method which is, only ocassionally, throwing an error (seems like every couple of emails regardless of the frequency). According to the docs, the getContent() method cannot be used in Apex email services.

When I do not invoke any email service my code works as expected 100% of the time, however it seems like when I attempt to send an email in the same context of when I use my getContent() method, an underlying SFDC bug occasionally is throwing the error: "FATAL_ERROR System.VisualforceException: null."

I have tried a lot of workarounds, including future methods, posting chatter feed posts to groups with emails on for every post, using a batch class and sending the email in the finish context, making HTTPS requests, calling another scheduable class, and more. I am hoping someone may have some ideas or can help lead me in the right direction. My code is below, thanks!

global class emailDeliverabilityStatus implements Schedulable {
global void execute(SchedulableContext ctx) {
    PageReference pr = new PageReference(Url.getSalesforceBaseUrl().toExternalForm() + '/email-admin/editOrgEmailSettings.apexp');
    Blob statusCheck = pr.getContent();
    Email_Status__c status = Email_Status__c.getall().values()[0];
    if (statusCheck != null && statusCheck.toString().contains('All email')) {
        status.deliverability_on__c = true;
        status.time_changed__c = system.now();
        List<String> sendTo = status.email_list__c.split(',');
        //Calls future method that sends the email to notify users
        sendEmailDelivNotification.sendEmail(sendTo, status.time_changed__c);
    } else if (statusCheck != null) {
        status.deliverability_on__c = false;
        status.time_changed__c = system.now();
    update status;
  • Do you really need to send the email from Apex code? If not, try sending it from process builder or workflow. – Hengky Ilawan Dec 10 '19 at 0:05
  • @HengkyIlawan Not even going to try that, it would cause the same issue because email service is still being invoked in the same context... Not to mention it would be pretty redundant to call a WFR or PB from Apex class/method, and keep in mind this is a scheduable class, that would significantly decrease performance of the process. – Max Goldfarb Dec 10 '19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.