2

I have used a apex:inputfile component and I have a button "Add more attachments" in my vf page. When I attach a file and click the add button, the attached file is gone. The page is reloaded everytime. The reloading of the page should not happen but I dont know why it is happening.

Here I am attaching my code. I have used a vf component.

<apex:component controller="MultiAttachmentController" allowDML="true">
<apex:attribute name="objId" type="String" description="The id of the object to manage attachments for" required="true" assignTo="{!sobjId}"/>
<apex:form id="attachmentForm">
    <apex:pageBlock title="Upload Attachments">
        <apex:repeat value="{!newAttachments}" var="newAtt">
            <apex:pageBlockSection columns="3">
               <apex:pageBlockSectionItem >
                    <apex:outputLabel value="File"/>                         
                    <apex:inputFile value="{!newAtt.body}" filename="{!newAtt.name}"/>
                </apex:pageBlockSectionItem>

            </apex:pageBlockSection>
        </apex:repeat>
        <apex:commandButton value="Add More" action="{!addMore}"/>

        <apex:commandButton value="Save" action="{!save}"/>
    </apex:pageBlock>
</apex:form>

vf page:

   <apex:page standardController="Account" extensions="MultiAttachmentController">
         <apex:pagemessages ></apex:pagemessages>
          <c:Multiattachment objId="{!$CurrentPage.Parameters.id}"/> 
    </apex:page>

Controller:

   public with sharing class MultiAttachmentController 
    {

public MultiAttachmentController(ApexPages.StandardController controller) {
    newAttachments = new List<Attachment>();
    newAttachments.add(attachment);
}


public Id sobjId {get; set;}
public Attachment attachment = new Attachment();

public List<Attachment> newAttachments {get; set;}

public MultiAttachmentController()
{  
    newAttachments = new List<Attachment>();
    newAttachments.add(attachment);
}   



public void addMore()
{
    Attachment newAttach = new Attachment();
    newAttachments.add(newAttach);

}    

// Save action method
public void save()
{
    List<Attachment> toInsert=new List<Attachment>();
    for (Attachment newAtt : newAttachments)
    {
        if (newAtt.Body!=null)
        {
            newAtt.parentId=sobjId;
            toInsert.add(newAtt);
        }
    }
    try{
        insert toInsert;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Inserted Attachments successfully'));
    }
    catch(Exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Exception'));
    }

}

}

1

The reason this is happening is because of how you've structured your controller. You didn't declare your public variables along with their Apex Properties (getters and setters) before your constructor. Consequently, your constructor has to fire every time you addMore(). Try reordering your controller code to something that looks more like this:

public with sharing class MultiAttachmentController 
{

public Id sobjId {get; set;}
public Attachment attachment = new Attachment();

public List<Attachment> newAttachments {get; set;}

// this constructor should probably be removed since you don't want to reference a 
// std controller in a component controller unless absolutely necessary (assumes will work w/o it)
public MultiAttachmentController(ApexPages.StandardController controller) {
    newAttachments = new List<Attachment>();
    newAttachments.add(attachment);
}

public MultiAttachmentController()
{  
    newAttachments = new List<Attachment>();
    newAttachments.add(attachment);
}   


public void addMore()
{
    Attachment newAttach = new Attachment();
    newAttachments.add(newAttach);

}    

// Save action method
public void save()
{
    List<Attachment> toInsert=new List<Attachment>();
    for (Attachment newAtt : newAttachments)
    {
        if (newAtt.Body!=null)
        {
            newAtt.parentId=sobjId;
            toInsert.add(newAtt);
        }
    }
    try{
        insert toInsert;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Inserted Attachments successfully'));
    }
    catch(Exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Exception'));
    }

}

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.