2

Ive been trying to add code to add a an attachment to my custom object. However the file, and filename keep showing up null (if I wrap "if file != null" around the attachment insert it bypasses it and inserts the object correctly), Im assuming it not being set, but I cant tell why. Relevent controller code:

public Blob file {get;set;} 
public String UploadedfileName {get;set;} 
public ApexPages.StandardController std;

public myController(ApexPages.StandardController stdController) {
    std = stdController;
}

public void submit() {
MapRequest__c cont=(MapRequest__c) std.getRecord();
insert cont;
Attachment att = new Attachment(); 
att.Body = file; 
att.Name = UploadedfileName; 
att.ParentId = cont.Id; 
att.Description = 'file uploaded.'; 
insert att; 

}

Relevant VF code:

<body>
    <apex:form>

    <apex:pageBlock title="Map Request Form" id="entireForm">
        <apex:pageBlockButtons id="buttons">
            <apex:commandButton status="pStatus" action="{!submit}"
                immediate="false" value="Submit" />
        </apex:pageBlockButtons>
      <apex:pageBlockSection columns="1" id="secondSection">
            <apex:pageBlockSection collapsible="true" columns="2"
                title="2nd Map Type" rendered="{!display2nd}">
                <apex:outputLabel value="Upload file 1" />  
                 <apex:inputFile value="{!file}" id="fileupload" fileName="{!UploadedfileName}"/> 
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="2nd Set Of Questions" for="dueDate" />
                    <apex:inputfield value="{!MapRequest__c.Due_Date__c}" id="dueDate" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlockSection>
     </apex:PageBlock>
    </apex:form>
 </body>

Note: both above codes are snippets and are likely missing a lot I just tried to grab relevant portions of the code and put them together in a meaningful format.

~~~~~EDIT~~~~~~~~~~~~~~~~~~~

I just found out that if I select a file and hit submit with one of the required fields left blank (which causes the page to trigger the whole "Error: required field missing" message box and stops submission. Then I fill in that required field and submit again, the submit will be successful and the file will upload. This implies hat the first time I hit submit it is setting the file's fields and the second time I hit submit its actually submitting. Any reason why it would do that??

6
  • I don't see a cause in the code you've posted. Is there just one page? (If there are multiple pages they must all reference the same controller to share state like UploadedfileName.) – Keith C Jul 21 '14 at 16:02
  • Just the one page, and I believe that is all that is relevant to the file upload. I have a feeling it has to do with the order of execution because if I, for example, move the line "insert cont" to after the line "att.ParentId = cont.Id;" then the page says the parentId is null just like the file and filename. – user2070057 Jul 21 '14 at 17:02
  • The ID of cont is only assigned when it is inserted the parent ID would be null if you do the insert after. You do select a file before clicking "Submit" don't you? – Keith C Jul 21 '14 at 17:33
  • Yes I select the file but I dont think the selecting of it is triggering the set function of it, I just edited the main post as to why I believe that. – user2070057 Jul 21 '14 at 17:44
  • Your question has the answer itself. You said that when u hit submit button leaving required field as blank then it gives error. Yes it will give error because validation rules run before submitting the form. Please read Order of Execution this will solve your problem. Here is the link salesforce.com/us/developer/docs/apexcode/Content/… – tandonprateek Sep 20 '14 at 12:26
1

Ok I found the issue, apparently inputFiles require some sort of page refresh to read the data properly. To do this I simply changed

<apex:form>

to

<apex:form enctype="multipart/form-data">

Not really sure what this changes at all but it makes it work.

0
0

I would have thought if you made your submit button return a pageReference - which was then null (ie. no redirect) this would work for you, as a more "typical" recipient of a submit action?

ie.

public pageReference submit() {
  MapRequest__c cont=(MapRequest__c) std.getRecord();
  insert cont;
  Attachment att = new Attachment(); 
  att.Body = file; 
  att.Name = UploadedfileName; 
  att.ParentId = cont.Id; 
  att.Description = 'file uploaded.'; 
  insert att; 
  return null;
}

You wouldn't need to change the whole form encoding then.

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.