9

We are trying to add attachments to an email template using a component. The code in the email template is:

<messaging:emailTemplate subject="My Subject" recipientType="User" relatedToType="My_Custom_Object__c">
<messaging:htmlEmailBody >

All,

blah, blah, blah

<apex:outputText value="If you have any questions, please email "/>
<apex:outputLink value="mailto:[email protected]"/>[email protected]
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >

All,

blah, blah, blah

</messaging:plainTextEmailBody>

<c:IncludeAttachments parentId="{!relatedTo.Id}" include="MostRecent"/>
</messaging:emailTemplate>

The component IncludeAttachments code is:

   <apex:component controller="IncludeAttachmentsController" access="global">

    <apex:attribute name="parentId"
        description="Parent Id"
        assignTo="{!parentObjectId}"
        type="Id" />

    <apex:attribute name="include"
        description="Include parameter"
        assignTo="{!includeAttachments}"
        type="String" />

    <apex:repeat value="{!attachments}" var="attachment">
        <messaging:attachment renderAs="{!attachment.contentType}" fileName="{!attachment.name}">{!attachment.body}</messaging:attachment>
    </apex:repeat>

    </apex:component>

The IncludeAttachmentsController code is:

global class IncludeAttachmentsController {

/* Variables and Constants */

global Id parentObjectId { get; set; }
global String includeAttachments { get; set; }

private static final String INCLUDE_ALL = 'All';
private static final String INCLUDE_MOST_RECENT = 'MostRecent';

public List<Attachment> attachments {
    get {
        if (INCLUDE_ALL.equalsIgnoreCase(includeAttachments)) {
            return [select 
                         name
                       , contentType
                       , body 
                    from Attachment 
                    where parentId = :this.parentObjectId];
        } else {  // include most recent
            return [select 
                         name
                       , contentType
                       , body 
                    from Attachment 
                    where parentId = :this.parentObjectId 
                    order by CreatedDate desc
                    limit 1];
        }
    }
    set;
}   

Two problems: The email link does not show up as a link, just text.

We get the following error:

Error: Invalid child of messaging:emailTemplate. Only messaging components are allowed as children of messaging:emailTemplate.

Can anyone help us resolve these two problems?

1 Answer 1

2

1) Error: Invalid child of messaging:emailTemplate. Only messaging components are allowed as children of messaging:emailTemplate.

This is because you are trying to use <messaging:attachment> inside the repeat tag in the component. this is not allowed. Are you trying to send the email with attachment in a .csv or PDF ( not giving the user the choice) if so I can rewrite your code to get a csv out.

2) Email link to show up as a link you need to change the output link to

<apex:outputLink value="mailto:[email protected]">mailto:[email protected] </apex:outputlink>

you have closed your outputlinktag before the mailto:[email protected] text.

5
  • Thanks, 1. I am using the repeat because there are multiple attachments. How do I handle that? 2. My typo: please see email link now.
    – JimH
    Jun 17, 2013 at 16:55
  • Having said the messaging:attachment has to be a child of messaging:emailTemplate we cannot use repeat over a messaging:attachment inside a tempalte.Will a email event work? check this post out : boards.developerforce.com/t5/Apex-Code-Development/…. Using the above you can make this a batch class and even schedule the emails
    – Rao
    Jun 17, 2013 at 17:01
  • Thanks for the answer to 2. Closing the tag worked. With regard to question 1, we have to do this with an email template.
    – JimH
    Jun 17, 2013 at 17:19
  • It would be hard to do with the template because 1) <attachment> needs to be a child of <messaging:> component. All I can suggest is taking a different approach metioned here ; salesforce.com/us/developer/docs/pages/Content/…
    – Rao
    Jun 17, 2013 at 17:23
  • @JimH were you able to find a solution for the multiple attachments? Apr 11, 2014 at 0:55

You must log in to answer this question.

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