When sending an Email via a Visualforce Email Template, I would like to get all attachments I have for an Opportunity record. I found this page very helpful: Insert Attachment into Visualforce Email Template When linking the documents, it works fine. But this piece of code does not:

<messaging:attachment filename="{!relatedTo.Attachments[0].Name}"   renderAs="{!relatedTo.Attachments[0].ContentType}">

When I try to save I get the error: Error occurred trying to load the template for preview: "Subscript is invalid because list is empty. Please try editing your markup to correct the problem"

What is the solution here, or how could I integrate one/more components depending on the list index of the attachment to get a similar result?


Subscript is invalid because list is empty makes it sound like your Attachments array is empty. Instead of hard-coding the first item in the array, why not use apex:repeat to iterate through them, or check Attachments.size first before outputting with <apex:outputPanel rendered="{!relatedTo.Attachments.size > 0}">{!relatedTo.Attachments[0].Id}</apex:outputPanel>

  • Well, this is the thing that you cannot use Apex:repeat with <messaging:attachment>. Take a look at the page I attached, there you can find the discussion. – A. SFDeveloper Jul 12 '17 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.