function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Geoffrey LambGeoffrey Lamb 

Upload attachment with a custom component

I have been using a basic upload method for about a year on a couple of different custom visualforce pages and it's been working successfully.

I am developing my first component and unfortunately, this same upload method is not working, nothing gets uploaded. When debugged, the body and name for the attachment are returning "null."

If my inputFile is located directly on a normal visualforce page, and my method on its controller, no problems. When I call it anywhere inside of this component, using the component's controller, the attachment.body and attachment.name fields return null. It's probably worth mentioning, that this input field is located inside of two repeating section. First, it loops through parent opportunities. And then for each parent, it creates and then loops through up to 4 possible attachment uploads. I have it set up so that the controller and page always creates all four of them, then they are hidden by jQuery. For the record, this repeat method works inside of my other VF pages. If I put it inside of a repeating opportunity loop on a component, it fails.

Then, I have an associated problem because even if I could get the body to NOT be null at upload, the system can't tell which parent opportunity to attach it to. I would like to simply pass the index of the parent and then get it by that number. But VF hate me, and won't let me pass anything it seems. If I try to do it as shown below, I am forced to add a rerender attribute. If i do that, I am told that I'm not allowed due to the inputFile field.

What’s going on, can someone help me please? I've looked everywhere, for days. I need to know how to upload on a component, inside of two repeat loops. And then, how to pass the index of the parent Opportunity so that the controller knows where to attach the document. I'm still new at all of this so hopefully, this might be something easy? Thanks so much for your help!

PS: The relevant code is below... I can post the entire thing if someone feels that would be helpful. Thanks again!
 
<!-- ==============  VISUALFORCE COMPONENT ============ -->
         <apex:repeat id="attRepeat" value="{!newAttachments}" var="a" >
                <tr id="upTable-{!indexNo}-{!attIndex}" class="upTable-Row">
                    <td class="upTable-DataLeft">
                        <apex:pageBlockSection id="pbsBrowse">
                            <apex:pageBlockSectionItem id="pbsiBrowse">
                                  <apex:inputFile id="browseBtn" styleClass="browseBtn" value="{!filebody}" filename="{!filename}"/>
                                
                            </apex:pageBlockSectionItem>
                        </apex:pageblocksection>
                    </td>
                </tr>
          </apex:repeat>

                
                    <apex:commandButton id="upBtn" styleClass="upBtn" action="{!upload}"  status="status" value="Upload Documents">
                        <apex:param name="indexOfOpportunity" value="{!o.indexNo}" assignTo="{!upIndex}" /> <!-- this won't pass either -->
                    </apex:commandButton>

<!-- ================ CONTROLLER ======================= -->

public class offerProController{


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


public offerProController(){
    
   for(Integer attsInList = 0 ; attsInList < 4 ; attsInList++){ // the '4' will be a variable after all is working
        newAttachments = new List<Attachment>{new Attachment()}; //instantiate list of 4 attachments for uploaded docs
   }
         
    }

public PageReference upload() {
    
           i = integer.valueof(Apexpages.currentPage().getParameters().get('indexOfOpportunity'));  // this variable won't pass
           offer = myOpportunityList.get(i)      

            for(attachment a : newAttachments){
            
            system.debug('#d attBody and name are: '+a.Body+' and ' + a.Name); // both null
            a.ParentId = offer.Id; // the Opportunity 
            try {
              insert a;
              return newPage;
            } catch (DMLException e) {
              ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
              return null;
            } finally {
              a = new Attachment(); 
              
            }
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
            return newPage;
        }return null;
    }

}