0

I have a page with uploading document form, I followed this example but I got a problem when I try to upload the file with the following error message Required fields are missing: [Document Name]

And here is my VF code

<apex:pageBlock title="Scan Document" >
  <apex:form id="scanDocumentTabForm" enctype="multipart/form-data">
            <apex:pageBlockSection columns="1" >
                  <apex:inputText value="{!document.name}"
                                  id="fileName"/>
                  <apex:inputFile value="{!document.body}"
                                  filename="{!document.name}"
                                  id="file"/>
                  <apex:inputField label="Description" value="{!document.description}"/>
                  <apex:actionRegion >
                       <apex:commandButton value="Upload File"
                                           action="{!saveDocument}"
                                           reRender="documentsTableForm"
                                           status="getDoc"/>
                  </apex:actionRegion>

            </apex:PageBlockSection>

            <apex:actionStatus startStyleClass="loading"
                               stopStyleClass="unload"
                               id="getDoc"/>

            <apex:pageMessages /> 
            <apex:pageBlockSection >
                  <apex:panelGroup id="documentsTable">
                        <apex:pageBlockTable value="{!documents}" var="doc" >
                             // table content .. 
                                    <!-- This is the dynamic reference part -->                    
                        </apex:pageBlockTable>
                  </apex:panelGroup>
            </apex:pageBlockSection>
    </apex:form>
</apex:pageBlock>

And this is the Controller Code

public Document document {get; set;}

public MyController (){
        document = new Document();
}

public pageReference saveDocument() {

        document.AuthorId = UserInfo.getUserId();
        document.FolderId = UserInfo.getUserId();

        try {
            insert document;

        } catch (DMLException e) {        
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));

        return null;
        } finally {
          document.body = null; // clears the viewstate
          document = new Document();
        }

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));

        return null;
    }
  • Why are you using action riegon? – Ashwani Feb 27 '14 at 9:19
  • Because I'm using commandLink with reRender, so I got error "Conjunction" in fileInput, Solved it with ActionRiegon from this source salesforce.stackexchange.com/questions/16837/… – user3003810 Feb 27 '14 at 9:27
  • Error : apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute. – user3003810 Feb 27 '14 at 9:37
0

Re-render don't work with <apex:input> but there is is no problem in page reload. On page refresh no information lost and data changes during action method call these details can be used on page

Here is an example using above sample code:

    public class DocumentUploader {
    public Document document {get; set;}
    public List<document> documents {get; set;}

    public DocumentUploader (){
            document = new Document();
            // Will show 3 results
            documents = [SELECT id, name, description FROM Document LIMIT 3];
    }

    public pageReference saveDocument() {

            document.AuthorId = UserInfo.getUserId();
            document.FolderId = UserInfo.getUserId();

            try {
                insert document;

            } catch (DMLException e) {        
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));

            return null;
            } finally {
              document.body = null; // clears the viewstate
              document = new Document();
              // This will load all document and refresh page. Constructor not reloads
              documents = [SELECT id, name, description FROM Document];
            }

            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));

            PageReference pref = ApexPages.currentPage();
            pref.setRedirect(false);
            return pref;
        }
}

Page:

<apex:page controller="DocumentUploader" tabstyle="Document">
<apex:form>
 <apex:pageblock>
    <apex:pageBlockSection columns="1" >
         <apex:pagemessages/>
         <apex:inputField value="{!document.name}" id="fileName"/>
         <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
         <apex:inputField label="Description" value="{!document.description}"/>
         <apex:commandButton value="Upload File" action="{!saveDocument}" status="getDoc" />
    </apex:PageBlockSection>
    <apex:pageblockTable value="{!documents}" var="doc">
      <apex:column value="{!doc.name}"/>
      <apex:column value="{!doc.description}"/>
    </apex:pageblockTable>
  </apex:pageblock>
 </apex:form>
</apex:page>
| improve this answer | |
  • Ok, but I have tabs and when the page refresh.. it's return to the first tab. – user3003810 Feb 27 '14 at 14:08
  • @user3003810 updated my answer. You can set tabstyle="document" in apex page as attribute – Ashwani Feb 27 '14 at 14:24
  • the same thing, I'll try to make selectedTab attribute for tabPanel dynamic, it may help .. thank you :) – user3003810 Feb 27 '14 at 14:42
0

In your saveDocument() method, before insert query, try

document.Name = 'ANY NAME OF YOUR CHOICE';

It should work.

UPDATE:

<apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
<apex:ActionRegion>
<apex:inputField value="{!document.name}" id="fileName"/>
<apex:inputField label="Description" value="{!document.description}"/>
<apex:commandButton value="Upload File"
                                       action="{!saveDocument}"
                                       reRender="documentsTableForm"
                                       status="getDoc"/>
</apex:actionRegion>
| improve this answer | |
  • Ok, It should work and works fine I tried it. but the document was saved without description, this means .. the form fields not work ! – user3003810 Feb 27 '14 at 10:14
  • Great! Can you put a debug statement in your constructor and see if it is getting called when you click on Upload File. It it is the case, try creating a getter method for document = new Document(); instead of putting that in constructor. – sfguru Feb 27 '14 at 10:43
  • I removed actionRegion , and the reRender attribute .. and it works fine but it is refreshing the whole page ! – user3003810 Feb 27 '14 at 10:54
  • Try to enclose actionRegion around the fields you have on form. When you put ActionRegion only the components inside it, will we processed by server. – sfguru Feb 27 '14 at 10:59
  • Visualforce Error apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute. – user3003810 Feb 27 '14 at 11:13

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.