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
chris_centrachris_centra 

upload attachment error - when in outputPanel with rendered...

hello.  odd behavior.  i have a vf page which allows the user to upload an attachment (standard inputFile stuff).  the attachment area is displayed conditionally - based on a controller variable.  when i browse to the page and try to upload an attachment, i get an error:  REQUIRED_FIELD_MISSING, Required fields are missing: [File Name, Body]: [File Name, Body].  However if i remove the "rendered" flag from the outputPanel, it works correctly.  what am i doing wrong?  all info is below.  thanks so much for your time.

 

thanks

chris 

 

the code is pasted below.  i tried to remove all unnecessary code...

 

VF Page is below.  offending "rendered" attribute in bold italics.  when the user clicks "CLICK ME", it calls the controller and flips the flag so that the inputFile area is displayed:

 

<apex:form >

<apex:outputPanel id="fullPagePanel">
<table >
<tr>

<td width="25%" valign="top">
<apex:outputPanel id="leftTablePanel">
<table >
<apex:actionRegion >
<apex:repeat value="{!itemWrapList}" var="itemWrap" id="itemWrapListRepeat">
<tr>
<td width="20%">
<apex:outputLink value="#">CLICK ME
<apex:actionSupport event="onclick" action="{!selectItemForEdit}" rerender="leftTablePanel, detailsPanelOuter, controllerMessagePanel, attachmentOuterPanel">
<apex:param name="selectedItemId" value="{!itemWrap.tmpId}" assignTo="{!itemIdForEdit}"/>
</apex:actionSupport>
</apex:outputLink>
</td>
</tr>
</apex:repeat>
</apex:actionRegion>
</table>
</apex:outputPanel>

<apex:outputPanel id="attachmentOuterPanel">
<apex:outputPanel id="attachmentPanel" rendered="{!AND(itemWrapForEdit != null, itemWrapForEdit.item.Id != null)}">
<apex:actionRegion >
<apex:outputText value="Input File: "/><apex:inputFile value="{!attachmentObj.body}" filename="{!attachmentObj.name}"/><br/>
<apex:commandButton value="Upload" action="{!upload}"/>
</apex:actionRegion>
</apex:outputPanel>
</apex:outputPanel>
</td>

</tr>
</table>
</apex:outputPanel>

</apex:form>

 

Apex below - upload function.  with the "rendered" attribute present, the attachmentObj is always empty, but without the "rendered" attribute, it works correctly:

 

 

public void upload() {
attachmentObj.ParentId = 'a0rM0000000KrVT';
insert attachmentObj;
attachmentObj = new Attachment();
return;
}

 

more Apex - selectItemForExit - which is what toggles the flag so that the inputFile is rendered...

public void selectItemForEdit() {
controllerMessage = null;
System.Debug(' #### in selectItemForEdit:'+itemIdForEdit);
if (itemIdForEdit != null) {
for (ItemWrapClass wrap : itemWrapList) {
System.Debug(' #### wrap.tmpId:'+wrap.tmpId);
if (itemIdForEdit == wrap.tmpId) {
// found
wrap.currentItemFlag = true;
itemWrapForEdit = wrap;

} else {
wrap.currentItemFlag = false;
}

}
}
System.Debug(' #### itemWrapForEdit:'+itemWrapForEdit);
}

Sgt_KillerSgt_Killer

Just try modifying your upload() funtion in controller as mentioned below :-

 

public void upload() {

attachmentObj = new Attachment(parentId = 'a0rM0000000KrVT', name=attachmentObj.name, body = attachmentObj.body);

insert attachmentObj;
return;
}

chris_centrachris_centra

Doesn't work.  attachmentObj.name and attachmentObj.body are blank (even though i choose a file), so writing them to a new copy of the object doesn't help.  same issue.

thanks

chris

Ishan K SharmaIshan K Sharma

Try something like this.

 

 public Attachment attachmentObj;

 

Public Attachment getattachmentObj()

attachmentObj = new Attachment();
return attachmentObj;

}

 

public Pagereference upload(){

Attachment attach = new attachment(parentId =  'a0rM0000000KrVT', name= attachmentObj .name, body = attachmentObj .body);
insert attach ;
return null;

}

 

in vf page

<apex:inputfile value="{!attachmentObj.body}" filename="{!attachmentObj.Name}" />

chris_centrachris_centra

maybe my original post wasn't clear.  this is exactly what is already in my code.  i'm using the standard approach for uploading an attachment, but the fact that it's in an outputPanel which is conditionally rendered, it doesn't seem to be behaving properly.

thanks

chris

Sgt_KillerSgt_Killer

I tried the your code and it gave the same error. But I was able to fix it just by modifying the code as shown below.

 

//////////////////////////////////////////////////////////////////////Controller Code///////////////////////////////////////////////////////////////

Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}

 

 

public void upload() {

 

Attachment attachmentObj = new Attachment();

attachmentObj.ParentId = '001i000000Ioh8B';
attachmentObj.name = myfile.name;
attachmentObj.body = myfile.body;
insert attachmentObj;
attachmentObj = new Attachment();
//return;
return NULL;

}

//////////////////////////////////////////////////////////////////////////////VF///////////////////////////////////////////////////////////////////////////

 

<apex:outputPanel id="attachmentOuterPanel">
<apex:outputPanel id="attachmentPanel" rendered="{!val1}">
<apex:actionRegion >
<apex:outputText value="Input File: "/><apex:inputFile value="{!myfile.body}" filename="{!myfile.Name}"/><br/>
<apex:commandButton value="Save" action="{!Savedoc}"/>
</apex:actionRegion>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageblocksection>