function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce sfdxsalesforce sfdx 

Hi Team , How to Automate the reports exported CSV File sent to Specified User Email

Hi Team, As i have an idea that yes, we can schedule the Report in Salesforce.

But Requirement is :
We have a report in salesforce ,  in the business hours for every one hour we have send the fresh Report of exported to CSV File and send to the specified User Email address .

How can i automate it that Report get automatically downloaded as csv and attached to an email and sent to specific user, 
Is that functionality Available in Salesforce.

Thanks 
Sfdx
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you check the below link for the similar solution.

https://salesforcecodex.com/salesforce/sending-report-as-attachment-in-salesforce/

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
salesforce sfdxsalesforce sfdx
Hi Praveen,
  Controller 1:
===========
public class ReportControllerTesting{
    
    //Generate report Based on parameter
    public void generateReport(string reportDevName)
    {
        try
        {
            List <Report> reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName =:reportDevName];
            if(reportList.size()>0)
            {
                String reportId = (String)reportList.get(0).get('Id');
                
                //Get Report Name
                string reportName=(String)reportList.get(0).get('Name');
                
                //get instance Url
                String instanceName = URL.getSalesforceBaseUrl().toExternalForm();
                
                string url=instanceName+'/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId;
                
                ApexPages.PageReference objPage = new ApexPages.PageReference(url);
                Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
                
                Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
                objMsgEmailAttach.setFileName(reportName+'.csv');
                objMsgEmailAttach.setBody(objPage.getContent());
                objMsgEmailAttach.setContentType('text/csv');
                email.setSubject(reportName);
                
                List<Messaging.EmailFileAttachment> attach=new List<Messaging.EmailFileAttachment>();
                attach.add(objMsgEmailAttach);
                email.setFileAttachments(attach);  
                
                EmailServiceTest service=new EmailServiceTest(email);
                service.body='Hello, <br/><br/> Here is attached '+reportName+'.<br/><br/>Thank You.<br/>Admin';
                service.isHtml=true;
                service.toAddresses=new List<string>();
                service.toAddresses.add('karimulla.shamim@gmail.com');
                service.toAddresses.add('syed.karimulla@techrbm.com');
                service.displayName='Salesforce Codex';
                service.subject=reportName;
                service.sendMail();
            }
        }
        catch(Exception ex)
        {
            system.debug(ex.getMessage());
        }
    }
  
}

Controller 2: For Email Service:
========================
 
public class EmailServiceTest {
   public string subject{get;set;} // mail subject
    public List<String> toAddresses{get;set;} // send to
    public List<String> CCAddresses{get;set;} // cc 
    public String body{set;get;} // mail plain body
    public string displayName{set;get;} // from 
    public boolean isAttachment {set;get;} // if we have to send attachment file in mail
    public Map<String,String> attachFiles {set;get;} // key = attachmentName and value =attachmentBody 
    public boolean isHtml{set;get;}
    public List<Messaging.EmailFileAttachment> attachments; 
    public Messaging.SingleEmailMessage email;
    
    public EmailServiceTest(){
        attachments = new List<Messaging.EmailFileAttachment>();
        email =new Messaging.SingleEmailMessage();
        this.isAttachment=false;  
        this.isHtml=false;
    }
    public EmailServiceTest(Messaging.SingleEmailMessage mailMessage){
        attachments = new List<Messaging.EmailFileAttachment>();
        email =mailMessage;
        this.isAttachment=false;  
    }
    public void sendMail(){
        
        if(isAttachment){
            for (String name : attachFiles.keySet()){                
                Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
                blob csvBlob = Blob.valueOf(attachFiles.get(name));
                string csvname=name+'.csv';
                csvAttc.setContentType('text/csv');
                csvAttc.setFileName(csvname);
                csvAttc.setBody(csvBlob);
                attachments.add(csvAttc);
            } 
        }
        email.setSubject(subject);
        email.setToAddresses(toAddresses);
        email.setSenderDisplayName(displayName);
        if(isHtml)
        {
            email.setHtmlBody(body);
        }
        else
        {
            email.setPlainTextBody(body);
        }
        if(isAttachment){
            email.setFileAttachments(attachments);  
        }
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
        system.debug('Email sent');
    }
}



The thing is report is working fine it seems, but the output iam getting the CSV but its encrypted it seems to be
Can you please let me know if anything to change 
Thanks 
Attached the picture.