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, Can any one suggest To get the Exact Report form apex class and download

Hi team, Iam using the below code to retrive the reportid and send the email message by converting the csv format.

But the Drawback is : it was not getting exact the reportat the time when  we Import by naviagation after clicking on the import csv button in salesforce.(Manually)

Iam using the below Code for reteriving but the thing is it was not adding the Grand Total and at the end iam getting the copyright message.


Do we have any suggestion to get exact report after running i need to call or do i need to modify anything in the code.

Please Refer the below Code:
//csv=1&exp=1&enc=UTF-8&isdtp=p1
    
    List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
             List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='test' limit 1];
            if(reportList.size()>0){
                for(Report report:reportList){
                    String reportId = (String)report.Id;     
                    string reportName=(String)report.Name;     
                    ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?csv=1&exp=1&enc=UTF-8&isdtp=p1');                    
                    Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
                    Date dt = System.today().toStartOfMonth().addMonths(-1);
                    System.debug(dt.format());
                    objMsgEmailAttach.setFileName(reportName+' '+dt.format()+' '+'.csv');
                    //if(!Test.isRunningTest())
                    objMsgEmailAttach.setBody(objPage.getContent());
                    objMsgEmailAttach.setContentType('text/csv');
                    attachments.add(objMsgEmailAttach);
                }
            }  
            
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'test@test.com'];
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) {
            mail.setOrgWideEmailAddressId(owea.get(0).Id);
            }
            mail.setUseSignature(false);
            mail.setToAddresses(new List<String>{'test.test@gmail.com'});
            mail.setSubject('Daily Report from Salesforce');
            mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
            mail.setFileAttachments(attachments);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


===============