0

We have created a custom component which will pull a report data and stream it with a comma delimiter. To send this data as an attachment we are referring this component in email template as below.

When we place the component reference in the email template, email is not getting delivered to the user. But when we remove the custom component from the template the email is delivering without any data in the attachment.

Component:

<apex:component controller="CSVStream" access="global">
    <apex:attribute name="xstrRptname" description="report ID" type="String" assignTo="{!strRptname}"/>
    <apex:outputText value="{!CSVStream}" escape="false"/>
</apex:component>

Related controller:

public class CSVStream {
    public static Boolean isTest;
    public static String strEmailAddr;
    public static String strOut;
    public static Boolean restRequested;
    public String strEmail{get;set;}
    public String strRptname{get;set;}

    void CSVStream () {
        strOut = '';        
        }

   public String getCSVStream() {
        restRequested = System.isFuture() || System.isScheduled();
        executeRpt();
        return strOut;
        }

    public void executeRpt() {
        String requestURL;
        requestURL = '/' + strRptname + '?csv=1&exp=1&enc=UTF-8';
        strOut = new PageReference(requestURL).getContent().toString();
        System.debug('CALLING executeRpt:  output= ' + strOut );
    }

}

Email template

Hi,

please find attached the report(s) you have requested...


Regards,
{!$Organization.Name}
</messaging:plainTextEmailBody>

<messaging:attachment filename="Account History - Account Type-{!Year(TODAY())}-{!MONTH(TODAY())}-{!Day(TODAY())}.csv" >
<c:ReportExportController xstrRptname=' *** Report id ***' />

</messaging:attachment>

</messaging:emailTemplate>

For debugging we have created a VF page and referred this component on this page and checked the preview which worked perfectly by showing the records from the report.

Is there anything missing in the process of defining the component in the template ??

1 Answer 1

0

In the Related controller code you have:

public String strRptname{get;set;}

Yet, in the email template you have:

<c:ReportExportController xstrRptname=' *** Report id ***' />

It doesn't appear to me that you want to get the strRptname in your controller, instead you only want to set it. It's possible the xstrRptname=' *** Report id ***' may be causing problems for you when you get it, causing an issue with the line below when it's set, preventing it from returning a correct value to your template:

<apex:attribute name="xstrRptname" description="report ID" type="String" assignTo="{!strRptname}"/> 

I also notice that you have a method below that appears to return a REST request for strOut:

   public String getCSVStream() {
        restRequested = System.isFuture() || System.isScheduled();
        executeRpt();
        return strOut;
        }

That somehow seems incongruent with the code below where you use a different method to create a pageReference named strOut:

public void executeRpt() {
    String requestURL;
    requestURL = '/' + strRptname + '?csv=1&exp=1&enc=UTF-8';
    strOut = new PageReference(requestURL).getContent().toString();
    System.debug('CALLING executeRpt:  output= ' + strOut );
}

At the beginning of your class, you define strOut as a string, not a pageReference:

public static String strOut;

These are inconsistent and need to be resolved for your code to work properly.

2
  • thanks for the points, yes I see the complete code is inconsistent because I copied this from one technical blog - But I am wondering how it worked when I do preview by referring the same on a VF page. As you stated I will change code accordingly and will try again [Reference blog - kevindotcar.wix.com/home/apps/blog/…
    – Rajasekhar
    Dec 28, 2016 at 16:32
  • 1
    I think the root cause of the issue is using getContent().toString(); function. Seems to be from version 34.0 salesforce is not supporting this function from triggers, test classes and apex email services
    – Rajasekhar
    Dec 28, 2016 at 18:12

You must log in to answer this question.

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