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
cduncombe44cduncombe44 

<apex:inputfile> issues...Body is always null

I'm having the hardest time doing something that should be relatively simple.  I have a VF component that is displaying a list of custom objects and a penel that will display details about each object.  For each child object in the details, I want the ability to upload an attachment.  I have used a wrapper class to help with this.  The issue is I am getting a VF error: Required field missig [Body] : [Body] every time.

 

Here is the relevant section of VF



<apex:repeat value="{!formWraps}" var="fWrap"> <tr> <td style="font-weight:bold"><apex:outputField value="{!fWrap.form.Form_Template__r.Name}"/></td> <td><apex:inputfield value="{!fWrap.form.Collected_Paper_Form__c}"/></td> <td><apex:inputfield value="{!fWrap.form.Attached_in_SalesForce__c}"/></td> <td><apex:outputLabel value="File: " for="file" style="font-weight:bold"/> <apex:inputText value="{!fWrap.attachment.Name}"/> <apex:inputFile value="{!fWrap.attachment.Body}" filename="{!fWrap.attachment.Name}"/></td> </tr> </apex:repeat>
<apex:pageblockButtons location="bottom">
      <apex:commandButton value="Save Changes" action="{!saveChanges}"/>
</apex:pageblockButtons>

 

  Here are the controller properties, methods, and wrapperClass that apply

public Contact con 					{get;set;}
public Event_Affiliation__c currentEvent 		{get;set;}
private list<Form__c> forms				{get;set;}
public list<formWrapper> formWraps			{get;set;}

..........

Id eventId = System.currentPageReference().getParameters().get('event');
currentEvent = [Select Id, Name, Pre_Registered__c, Attended__c, Fishing_Partnership_Event__c,
Fishing_Partnership_Event__r.Name, (Select Id, Name From Attachments), Fishing_Partnership_Event__r.Location__c,
Fishing_Partnership_Event__r.Event_Date__c From Event_Affiliation__c Where Id = : eventId]; forms = [Select Id, Name, Form_Template__c, Form_Template__r.Name, Collected_Paper_Form__c, Attached_in_SalesForce__c,
Attachment_ID__c, Event_Affiliation__c From Form__c Where Event_Affiliation__c =: currentEvent.Id]; for(Form__c f : forms){ formWraps.add(new formWrapper(f)); } ............... public PageReference saveChanges(){ for(formWrapper wrap : formWraps){ wrap.attachment.Name = wrap.form.Form_Template__r.Name + ' ' + con.Name; wrap.attachment.ParentId = wrap.form.Event_Affiliation__c; wrap.attachment.OwnerId = UserInfo.getUserId(); wrap.attachment.Body = fileBody; system.debug('attachment Name: ' + wrap.attachment.Name);
system.debug('attachment OwnerID: ' + wrap.attachment.OwnerId);
system.debug('attachment ParentID: ' + wrap.attachment.ParentId); system.debug('attachment Body: ' + wrap.attachment.Body); insert wrap.attachment; } try{ update currentEvent; update healthInts; }catch(Exception e){ ApexPages.addMessages(e); } PageReference page = new PageReference('/' + currentEvent.Id); page.setRedirect(true); return page; } .................. /*WRAPPER CLASS*/ public class formWrapper{ public Form__c form {get;set;} public string attachmentID {get;set;} public Attachment attachment {get;set;} public formWrapper(Form__c f){ form = f; attachmentID = ''; attachment = new Attachment(); } public formWrapper(Form__c f, string aID, Attachment a){ form = f; attachmentID = aID; attachment = a; } }

 

Here is the Debug logs...for some reason the body of the attachment is not being bound from the input file to the attachment within the wrapper class, all the other attributes are being set....I've been pouding my head on the wall for a while with this one

09:58:45.341 (341358000)|SYSTEM_METHOD_ENTRY|[63]|System.debug(ANY)
09:58:45.341 (341376000)|USER_DEBUG|[63]|DEBUG|attachment Name: Signed Consent Form Johnny Appleseed
09:58:45.341 (341384000)|SYSTEM_METHOD_EXIT|[63]|System.debug(ANY)
09:58:45.341 (341403000)|METHOD_ENTRY|[64]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341422000)|METHOD_EXIT|[64]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341466000)|SYSTEM_METHOD_ENTRY|[64]|String.valueOf(Object)
09:58:45.341 (341480000)|SYSTEM_METHOD_EXIT|[64]|String.valueOf(Object)
09:58:45.341 (341492000)|SYSTEM_METHOD_ENTRY|[64]|System.debug(ANY)
09:58:45.341 (341502000)|USER_DEBUG|[64]|DEBUG|attachment OwnerID: 005d00000011zsxAAA
09:58:45.341 (341510000)|SYSTEM_METHOD_EXIT|[64]|System.debug(ANY)
09:58:45.341 (341524000)|METHOD_ENTRY|[65]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341542000)|METHOD_EXIT|[65]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341620000)|SYSTEM_METHOD_ENTRY|[65]|String.valueOf(Object)
09:58:45.341 (341640000)|SYSTEM_METHOD_EXIT|[65]|String.valueOf(Object)
09:58:45.341 (341661000)|SYSTEM_METHOD_ENTRY|[65]|System.debug(ANY)
09:58:45.341 (341676000)|USER_DEBUG|[65]|DEBUG|attachment ParentID: a0gK00000011xBeIAI
09:58:45.341 (341686000)|SYSTEM_METHOD_EXIT|[65]|System.debug(ANY)
09:58:45.341 (341701000)|METHOD_ENTRY|[66]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341721000)|METHOD_EXIT|[66]|01pK00000001Qv8|EventAffiliationListController.formWrapper.__sfdc_attachment()
09:58:45.341 (341734000)|SYSTEM_METHOD_ENTRY|[66]|String.valueOf(Object)
09:58:45.341 (341741000)|SYSTEM_METHOD_EXIT|[66]|String.valueOf(Object)
09:58:45.341 (341753000)|SYSTEM_METHOD_ENTRY|[66]|System.debug(ANY)
09:58:45.341 (341762000)|USER_DEBUG|[66]|DEBUG|attachment Body: null
09:58:45.341 (341769000)|SYSTEM_METHOD_EXIT|[66]|System.debug(ANY)

 

I know this is bits and pieces, and totaly not best practices with SOQL in for loops and such, but that wouldnt cause the Body of the attachment to be null.  I figure I want to get it working before worrying about optimizing the code.  

 

Any help would be greatly appreciated.  Just hoping there is something obvious that I am missing that someone could point out.  PLEASE HELP

 

Chris

 

bob_buzzardbob_buzzard

In your savechanges method you are overwriting the body with another property:

 

wrap.attachment.Body = fileBody;

You shouldn't need to do anything to the body once uploaded. 

 

 

cduncombe44cduncombe44

Thats leftover from another effort I had to bind the input file body to a blob fileBody, and then overwrite the attachment body with that property, but that didnt work, and it still doesnt work when I bind the input file directly wrap.attachment.body.  Im clueless as to why.

Starz26Starz26

If you comment out the line where you are assigning the fileBody to the body and do nothing elsedoes it work?

 

This code works as ecpected (modified from yours):

 

class

 

public class testPageController{

private list<Contact> forms             {get;set;}
public list<formWrapper> formWraps          {get;set;}

            
public testPageController(){       
 forms = [Select ID From Contact];
  formWraps = New formWrapper[]{};      
for(Contact f : forms){
   
        formWraps.add(new formWrapper(f));              
}
}


public PageReference saveChanges(){ 
        
        for(formWrapper wrap : formWraps){
            if(wrap.attachment.body == null) continue;
            wrap.attachment.Name = 'test';
            wrap.attachment.ParentId = forms[0].id;
            wrap.attachment.OwnerId = UserInfo.getUserId();
            //wrap.attachment.Body = fileBody;
            system.debug('attachment Name: ' + wrap.attachment.Name);
                system.debug('attachment OwnerID: ' + wrap.attachment.OwnerId);
                system.debug('attachment ParentID: ' + wrap.attachment.ParentId);
            system.debug('attachment Body: ' + wrap.attachment.Body);
            insert wrap.attachment;
            
            
        }
        
                 
    
    
    return null;
    }


/*WRAPPER CLASS*/
public class formWrapper{
        
        public Contact form         {get;set;}
        public string attachmentID      {get;set;}
        public Attachment attachment        {get;set;}
        
        
        public formWrapper(Contact f){
            form = f;
            attachmentID = '';
            attachment = new Attachment();
        }
        public formWrapper(Contact f, string aID, Attachment a){
            form = f;
            attachmentID = aID;
            attachment = a;
        }
        
    }


}

 

VF Page

 

<apex:page controller="testPageController">
<apex:form>
  <table>
  <apex:repeat value="{!formWraps}" var="fWrap">
     <tr>
        
                
        <td><apex:outputLabel value="File: " for="file" style="font-weight:bold"/>
            <apex:inputText value="{!fWrap.attachment.Name}"/>
            <apex:inputFile value="{!fWrap.attachment.Body}" filename="{!fWrap.attachment.Name}"/></td>                 
    </tr>
</apex:repeat>
</table>

      <apex:commandButton value="Save Changes" action="{!saveChanges}"/>

</apex:form>
</apex:page>

 So if commenting out that line does not work, it is somewhere else in your code.

 

 

 

Kirill_YunussovKirill_Yunussov
Well, I just spent an hour banging my head against the wall trying to figure out why my file body was null.  Everything looked fine, and I tried a number of different approaches with storing the body in separate variables, etc..  Finally, I noticed that I did not have an exclamation sign in the "value" attribute of the <apex:inputfile>.   The page compiled and saved without showign any errors, so this did not come to my attention until a while later..  Salesforce should probably fix that.  Or I should start typing better.

<apex:inputFile filename="{!fWrap.attachment.Name}" value="{fWrap.attachment.Body}"/>