0

With this code im only able to get a corrupted xlsx file in email. i have attached the image of cvs converted format.enter image description here

public static void SendEmailNotification(string body,string ReportName, Id ReportId){

    ApexPages.PageReference objPage = new ApexPages.PageReference('/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+ReportId);
    system.debug('ObjPage :'+objPage);
    
   
   
    List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
    Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
        objMsgEmailAttach.setFileName(ReportName+'.csv');
        objMsgEmailAttach.setBody(objPage.getContent());
    system.debug('obj content 1 :'+Blob.valueof(objPage.getContent().toString()));
        objMsgEmailAttach.setContentType('text/csv');
        List<Messaging.EmailFileAttachment> attach=new List<Messaging.EmailFileAttachment>();
        attach.add(objMsgEmailAttach);
        email.setFileAttachments(attach);
    
    
    List<String> Recipients = New List<String>();
    
    email.setToAddresses(Recipients);
    email.setSubject('Custom Email Notification');
    email.setHTMLbody(Body);
    emailList.add(email);
    system.debug('email sent');
    Messaging.SendEmailResult[] EmailResults = Messaging.sendEmail(emailList);
    system.debug('Email result:'+EmailResults);
}

1 Answer 1

0

Since there is an api call for running the report in the same class method, The callout for Report attachment should be written under @Future(callout=true) annotation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .