I have a VF page that allows users to upload a file (attachment for email). The name and value of this file are then saved to attachment1, a variable to store the inputted file values. In addition to the uploaded attachments, an email template attachments are displayed. Relevant VF page code:

<apex:pageblock title="Attachments">
     <apex:inputFile value="{!attachment1.body}" filename="{!attachment1.name}" id="file"/>
          <apex:commandButton value="Save Attachment to List" action="{!attach_new}"/>
               <apex:pageblocktable value="{!Selectedattachments}" var="a">
                      <apex:column headervalue="Attachment Name" value="{!a.acc.name}"/>
                      <apex:column headerValue="Attachment Id"   value="{!i}"/>

In the controller, I have a wrapper class that helps fill the VF page with the list of attachments - this list consists of (1) attachments pulled from an email template and (2) attachments that are uploaded dynamically with the click of a button ("Save Attachment to List")

 public List<attachwrapper> GetSelectedattachments()
                    return getattach();                    
                    return getattach();

        public List<attachwrapper> getattach()
                             if(selectedattach.size() > 0){
                                attach_criteria = [select id,name from attachment where Id NOT IN: selectedattach AND parentId =: '00X50000001XtXB'];
                                if(uploadList.size() > 0){
                                  attach_criteria = [select id,name from attachment where ParentId =: '00X50000001XtXB'];
                                  if(uploadList.size() > 0){
                             for(attachment a : attach_criteria){          
                             attach.add(new attachwrapper(a));
                             return attach;

     public class attachwrapper{
                        public attachment acc{get; set;}
                        public Boolean selected{get; set;}
                        public attachwrapper(attachment a){
                        acc = a;
                        selected = false;
    public pagereference attach_new(){
                        attachment1.OwnerId = UserInfo.getUserId();
                        attachment1.ParentId = '00X50000001XtXB';
                        return null;

When I upload one attachment, it works well. The uploaded attachment (say name ABC) gets appended to the list of displayed attachments on the VF page. However, when I upload a second attachment (name XYZ), the second attachment gets uploaded, but attachment ABC gets renamed as XYZ. The last uploaded attachment renames the earlier uploaded attachments.

To illustrate, the following shows the first three attachments initially pulled from an email template while column width is a manually uploaded attachment thru the inputFile component. This works fine. enter image description here

Now when I upload a second attachment named backupCode:

enter image description here

How can I modify the code so that the uploaded attachments don't get replaced by the latest uploaded attachment file? Appreciate your help.


You must add "attachment1 = new Attachment();" into "attach_new" method just before the return null; That should fix your problem

| improve this answer | |
  • Yes, of course, the instantiation! – Rosh Aug 13 '13 at 6:07

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.