+ Start a Discussion
Sam SatterfieldSam Satterfield 

Apex Trigger to call Apex Class that send an email w/ attached csv report

I was initially, and still am, trying to schedule an Apex class to send this; but quickly realised that ".getContent()" wouldn't let me. I changed tactics to have it send via trigger, that called the class, and would then send the email w/ csv attached. I would then either schedule a class to update the object tied to the trigger, or a workflow to update it.

unfortunatly, the report it send looks like this:
Messed up report

I've figured that something about going through a trigger does this; as a button works just fine.

I will include my code below, and I appreciate any help. Thanks.

trigger Call4Health_Export on Analytics__c (before Update)
For(Analytics__c Ana: Trigger.new)
global class Call4Health_Export implements Database.AllowsCallouts{
    webservice static void Call4Health_Export(Id recId){    
        ApexPages.PageReference report = new ApexPages.PageReference('/00O19000000VSV2?csv=1');     
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Lead Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'ssatterfield@femwell.com' } );
        message.setCcAddresses(new String[] { 'ssatterfield@femwell.com'});
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );

Hi Sam,

Please share your VF page code also.

Do you have showHeader="false" and sidebar="false" in your Visualforce page?

Like this:
<apex:page showHeader="false" sidebar="false">

Sam SatterfieldSam Satterfield
I do not have any VF pages set up for our org.,I have yet to need one. If one is needed, could please provided any elaboration as to why?