0

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 it's 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>
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;
}

}
  • Is there any way that you can make the question concise or just make in Bullet form for us to read quickly. I literally didn't dare to even read through seeing such a long question with so many paragraphs and details. – Mahmood Dec 9 '14 at 17:27
  • Yeah, I know where you're coming from... but I tried to re-write it a couple of times and this is what i got, everything within it is necessary. I doubt anyone can help or would have time to dig through it all to understand the problem, it's pretty complicated. But if anyone can, this is sort of a last ditch effort, I never post questions like this. – ShortSaleGuy Dec 12 '14 at 12:03
1

I finally figured this out. The solution was to move the upload portion of the page to a separate page and then include that upload page in the original parent via iFrame. This allowed me to restore the rerender functionality to the main page. As for the name and body returning null, I created an inner class for the attachments, and then I was able to bind the name and body to the classes variables. Now, all works as intended! It's a lot of code since this now requires two pages, a component and two controllers - so I won't post it all. But if anyone has a similar problem with uploading attachments in a list that are returning null or with unexpected results, I'd be happy to help. Thanks to anyone who read this and thought about helping me :)

| improve this answer | |
  • Can you please share your approach? Please? I need a similar kind of thing. – Testing_SFDC Oct 5 '18 at 12:30

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.