+ Start a Discussion
Uma PrabhakarUma Prabhakar 

Email Reports with Attachments

Hello Experts

I am New to the Salesforce Ecosystem, i am trying to design a functionality where custom reports needs to be sent via Email With attachment, i know this beta version, but i need to design through customization, i have researched a lot and finally came up with the link

https://blogs.absyz.com/2018/09/27/sending-reports-to-email-as-an-attachment/

i have done exactly the same but dont know why its not working, could any one 

Visualforce Component
 
<apex:component controller="ReportsToEmailController" access="global">
    <apex:attribute name="ReportId" description="Report ID" type="Id" assignTo="{!rptId}"/>
    <apex:outputPanel>         
        <table style="width: 100%;">
            <thead>                 
                <apex:repeat value="{!ReportResult.reportMetadata.detailColumns}" var="colName"> 
 
                    <!-- reportMetadata is a class where it contains metadata of a report.
                        DetailColumns is a method of ReportMetadata class, it returns the API names (Columns Names) 
                            for the fields that contain detailed data-->
 
                    <th><apex:outputText value="{!ReportResult.reportExtendedMetadata.detailColumnInfo[colName].label}"/></th>
 
                    <!-- reportExtendedMetadata is class where it contains Report extended metadata and
                            it provides data type and label information.
                        detailColumnInfo is a method of reportExtendedMetadata class, it returns map of columns names 
                            and its label to Display as Header -->
 
                </apex:repeat> 
            </thead> 
            <tbody> 
                <apex:repeat value="{!ReportResult.factMap['T!T'].rows}" var="row" rows="999"> 
                    <!-- Here we will get entire data of each row and T refers to the Row -->
                    <tr> <apex:repeat value="{!row.dataCells}" var="cell">
                        <!-- Here we will get data of each cell and displayed -->
                        <td><apex:outputText value="{!cell.label}"/></td>
                        </apex:repeat> </tr> 
                </apex:repeat>
            </tbody>
        </table>
    </apex:outputPanel> 
</apex:component>

Apex Controller: ReportsToEmailController
 
public class ReportsToEmailController {
    public Id rptId { get; set; } // Here we will get the report Id from the VF Component
    private transient Reports.ReportResults results; // It will hold the entire data of a report
 
    /*********************
     // Method Name : getReportResult
     // Description : Here we will get the data of a report and send to the VF Component
    /********************/
 
    public Reports.ReportResults getReportResult() {
        // Here it will run the report with that report ID and adding the report data into results
        results = Reports.ReportManager.runReport(rptId, true);
        return results;
    }
}

My custom VF template where 
 
<messaging:emailTemplate subject="Report" recipientType="User" >
    <messaging:plainTextEmailBody >
        Hi {!recipient.FirstName}
        Please find the below attachments. 
 
    </messaging:plainTextEmailBody>
    <messaging:attachment filename="Opp Record Report.xls">
        <!-- Here we can add multiple Reports -->
        <c:ReportsToEmail ReportId="00O2w000003DegwEAC"/>
        <c:ReportsToEmail ReportId="00O2w000003DegwEAC"/>
    </messaging:attachment>
</messaging:emailTemplate>


Next i have Designed workflow rule where when checkbox update  with Email alert, but i am not recieveing any mails, Any help will be highly appreciated


 
ShirishaShirisha (Salesforce Developers) 
Hi Uma,

Can you please check,if you have set the deliverability to All email to trigger the email whenever the workflow fired.

Also,I would suggest you to capture the debug logs to see,if there is any specific errors on this.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Uma PrabhakarUma Prabhakar
@Shirisha

Thank you for the Help, i resolved the issue, i was able to design a workflow where report should be sent with csv attachment, i am getting a email but the attachment shows as corrupted

This is what it should look like

User-added image

This is what it looks now

User-added image

Do you have any suggestion on this?