I am trying to upload a file from VF page and it is always showing null.

Here is my code

VF Page

<apex:form > 
<apex:pageblock title="Walkthrough Completion">
    <apex:pageBlockSection >

        <apex:inputfield value="{!fs.Did_FO_Take_Ownership__c}" /> 

    <apex:pageBlockSection columns="1">
            <apex:inputfield value="{!fs.Walk_Through_Notes__c}" style="width : 500px; height:75px;" required="true"/>

    <div class="pbSubsection">
        <table class="detailList" border="0" cellpadding="0" cellspacing="0">
                    <th class="labelCol vfLabelColTextWrap  first  last " scope="row">
                    <label for="j_id0:j_id29:j_id30:j_id36:j_id37">
                    <span class="assistiveText">*</span>Attach Account Acceptance Form</label></th>
                    <td class="data2Col  first  last ">
                       <apex:inputFile value="{!Attach}" filename="{!fileName}"/>
  <apex:pageBlockButtons >
         <apex:actionRegion > 
        <apex:commandButton value="Save" action="{!save}" rerender="showmsg" />
        <apex:commandButton value="Return to Account" action="{!cancel}"  /> 


public class CVFE_Walkthroughcompleted {
public Account acct{get;set;}

public blob Attach {get; set;}  // For attaching file to the specific record
public String fileName {get; set;}
public  Attachment a {get; set;}

public CVFE_Walkthroughcompleted(ApexPages.StandardController stdController)
    a = new Attachment();
    Set<id> ServicesId = new Set<id>();
    List<String> fieldnames = new list<String> {  'OnBoarding_Stage_Nos__c' , 'Onboarding_Status__c' ,'ownerid'};
    this.acct = (Account)stdController.getRecord();


public pagereference save()
    System.debug('Attach is ' + Attach); // this is always null
    System.debug('filename is ' + filename);// this is always null
    system.debug('newFSHs ' + newFSHs.size());
    Savepoint sp = Database.setSavepoint();
        List<Attachment>  fshAtt = new List<Attachment>();

        else if(newFSHs.size() > 0)
            Date fshstartDate = Date.today();

            for(Franchise_Service_History__c fsh : newFSHs)
                fsh.Was_FO_on_Time__c = fs.Was_FO_on_Time__c;


            if(newFSHs.size() > 0)

                if(this.acct.OnBoarding_Stage_Nos__c < 60  )
                    system.debug('Time to update ' );
                    this.acct.Onboarding_Status__c = 'Walkthrough completed';
                    for(Franchise_Service_History__c fsh :newFSHs)

                    //System.debug('Attach is ' + Attach);
                    System.debug('A is ' + a);
                    if(Attach  != null){                    

                    update newFSHs;
                    update this.acct;
                    insert fshAtt;

                    Task t = new Task(subject='ONBOARDING: First Clean Call to New Customer', ActivityDate=fshstartDate.addDays(1),status='Open',WhatId=this.acct.Id,type='Call',OwnerId = this.acct.ownerid);
                    insert t;
    }catch(exception e)
        System.debug('There was an error..'+ e.getmessage());
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'There was an error.. ' + e.getmessage()));
        return null;

    return null;


  • 1
    I believe we will require some more information about where are you getting a null. Also I see that the else if(newFSHs.size() > 0) should be preceded by an IF – manjit5190 Aug 16 '16 at 7:03
  • Oh!! that was a typo, i had removed part of the code which was unwanted and trying to stick to only the relevant piece of code . As for the null i have marked that on the code as comment. When the save button is hit the values are null – Prady Aug 17 '16 at 9:08

You can bind the attachment instance to the VF page and insert the same instance into the data base.

The body attribute of Attachment will hold the file.

See the example below for a reference:


public with sharing class AttachmentController {

  public Attachment attachObj {get;set;}

  public AttachmentController(){
    attachObj = new Attachment();

  public PageReference saveAttachment() {

    attachObj.ParentId = 'xxxxxxxxxxxx'; //  parent record id for an Attachment
    try {
      insert attachObj;
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
      return null;

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;

VF Page:

<apex:page controller="AttachmentController">  
    <apex:pageMessages />
    <apex:pageBlock title="Save a Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!saveAttachment}" value="Save"/>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachObj.name}" id="fileName"/>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachObj.body}" filename="{!attachObj.name}" id="file"/>



enter image description here

  • Thanks @devendra. I did try that before and even then its null. The same piece of code was used as managed package and is working well there. I just copied over that code and it throws this issue. – Prady Aug 17 '16 at 9:14
  • @Prady I have used the above code and checked the Name and Body attribute of Attachment. Those are appearing as Not NULL values. – Devendra Aug 17 '16 at 10:40
  • I had used the same code on a managed package as well where its working well. Cant figure out what could be issue here – Prady Aug 17 '16 at 10:48

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.