+ 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:
trigger Call4Health_Export on Analytics__c (before Update)
{ 
For(Analytics__c Ana: Trigger.new)
{Call4Health_Export.Call4Health_Export(Ana.Id);}
}
Class:
global class Call4Health_Export implements Database.AllowsCallouts{
    @future(callout=true)
    webservice static void Call4Health_Export(Id recId){    
        if(recId!=null)
        {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O19000000VSV2?csv=1');     
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        attachment.setContentType('text/csv');
        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 } );
        }}}


 
Vishal_GuptaVishal_Gupta
Hi Sam,

Please share your VF page code also.

Thanks,
Vishal
ClintLeeClintLee
Do you have showHeader="false" and sidebar="false" in your Visualforce page?

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

Clint
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?