I am posting my approach, but still unable to send the VF page data as email attachment.
scheuler + batch :
global class Schedule_report implements Schedulable
{
global void execute(SchedulableContext ctx)
{
list<EmailTemplate> lstEmailTemplates = [select Id,Name,DeveloperName from EmailTemplate where Name=: 'some_template'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if(lstEmailTemplates!=null && lstEmailTemplates.size()>0)
mail.setTemplateId(lstEmailTemplates[0].Id);
mail.setSaveAsActivity(false);
mail.setToAddresses(new list<string>{'rao@gmail.com'});
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
@isTest
global static void testSchedule()
{
Test.startTest();
Schedule_report sched = new Schedule_report ();
Id job_id = System.schedule('test', '0 0 0 30 12 ? 2099', sched);
System.assertNotEquals(null, job_id);
Test.stopTest();
}
}
Email template :
<messaging:emailTemplate subject="test subjects" recipientType="User" relatedToType="Opportunity" >
<messaging:HTMLEmailBody >
<c:custom_component />
</messaging:HTMLEmailBody>
</messaging:emailTemplate>
Component :
<apex:component controller="excel_controller" access="global" >
<apex:datatable value="{!Report_for_template}" var="opp">
<apex:column headervalue="Opportunity Name">{!opp.name} </apex:column>
<apex:column headervalue="Opportunity owner">{!opp.owner.name}</apex:column>
<apex:column headerValue="Opportunity current Quarter">{!opp.Expected_Close_Quarter_1__r.name}</apex:column>
<apex:column headervalue="Opportunity Owner Emp ID" value="{!opp.owner.EmployeeNumber}"/>
<apex:column headervalue="Opportunity Region " value="{!opp.Region__c}" > </apex:column>
<apex:column headervalue="Account related to Oppty " value="{!opp.account.name}"></apex:column>
<apex:column headervalue="Account Owner" value="{!opp.account.owner.name}" > </apex:column>
<apex:column headervalue="Account Owner Emp ID" value="{!opp.Account.owner.EmployeeNumber}"/>
</apex:datatable>
</apex:component>
The above approach / using native HTML with repeat does not help me out
when I change the Email template to :
<messaging:emailTemplate subject="test subject" recipientType="User" relatedToType="Opportunity" >
<messaging:HTMLEmailBody >
hello see the attachment
</messaging:HTMLEmailBody>
<messaging:attachment>
<c:custom_component />
</messaging:attachment>
the contents inside the attachments can be rendered as PDF which seems to do the job but getting to make it show up as a xls/csv seems to be impossible :(
</messaging:emailTemplate>
when I use datatables inside the component and use I get the csv as :

when I use plain HTML + inside the component body and use I get the csv as

the way salesforce uses the .csv is more like a drop down list that says
customer here is the info
name : name 1
address : address 1
name : name 2
address : address 2
When it comes to making the data fit in excel columns it seems impossible to map the data.( I was able to render this report as PDF, that is never an issue).
Questioning myself on how to map the data from datatables/table to an excel document :O. Is this even possible?
this is what I am looking for in an excel/csv format.
