Salesforce Apex类适用于经典版本,但不适用闪电版本

时间:2018-12-13 17:31:13

标签: salesforce apex salesforce-lightning

大家情况如何?因此,为了直截了当,我的任务是尝试修复一个Apex类,该类虽然在Salesforce中可以正常运行,但在Lightning中根本不起作用。 Apex类应该允许用户安排导出任何指定的报告。如果有人可以看看并给我一些提示,将不胜感激。

global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        DoExport();
    }
    @AuraEnabled
    @future(callout=true)
    public static void DoExport(){
        List<Report_Export__c> exportList = new List<Report_Export__c>{};
        List<Report_Export__c> exportUpdateList = new List<Report_Export__c>{};
        List<Messaging.SingleEmailMessage> mails  = new List<Messaging.SingleEmailMessage>();
        Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
        Date d = Date.today();
        Date firstDate = d.toStartOfMonth();
        Date lastDay = firstDate.addDays(Date.daysInMonth(d.year(), d.month())-1);

        system.debug('First Day: ' + firstDate);
        system.debug('Last Day: ' + lastDay);

        exportList = [Select Id, Name, Report_ID__c, Attachment_Name__c, Email_Subject__c, Email_Body__c, Email_Recipients__c, Frequency__c, Weekly_Day__c, Monthly_Day__c, Last_Ran__c from Report_Export__c];

        for(Report_Export__c e : exportList){
            //Determine if Exporter record is scheduled to run today.
            Boolean process = Test.isRunningTest() ? true : false;

            //Test for Weekly frequency.
            process = e.Frequency__c == 'Weekly' && e.Weekly_Day__c.contains(dt.format('EEEE')) ? true : process;

            //Test for Monthly frequency.
            process = e.Frequency__c == 'Monthly' && (e.Monthly_Day__c == String.valueOf(d.day()) || e.Monthly_Day__c == 'last' && d == lastDay || lastDay.day() <= Integer.valueOf(e.Monthly_Day__c)) ? true : process;

            //Run process if scheduled.
            if(process){
                System.debug('Starting message processing for: ' + e.Name);
                ApexPages.PageReference report = new ApexPages.PageReference('/' + e.Report_ID__c + '?csv=1');
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
                attachment.setFileName(e.Attachment_Name__c);
                Blob content = Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : report.getContent();
                attachment.setBody(content);
                attachment.setContentType('text/csv');
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
                message.setSubject(e.Email_Subject__c);
                message.setPlainTextBody(e.Email_Body__c);
                String[] emailRecipients = e.Email_Recipients__c.split(',');
                message.setToAddresses(emailRecipients);
                mails.add(message);
                System.debug('Completed message processing for: ' + e.Name);

                //Update Last Ran date and add to update list.
                e.Last_Ran__c = System.now();
                exportUpdateList.add(e);
            }
        }
        //Send the email messages all at once to avoid limits.
        Messaging.sendEmail(mails);
        System.debug('Send emails.');

        //Update Export record with Last Ran date.
        update exportUpdateList;
    }
}

0 个答案:

没有答案