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>
<apex:pageBlockSection columns="1">
<apex:inputfield value="{!fs.Walk_Through_Notes__c}" style="width : 500px; height:75px;" required="true"/>
</apex:pageBlockSection>
<div class="pbSubsection">
<table class="detailList" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<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}"/>
</td>
</tr>
</tbody>
</table>
</div>
<apex:pageBlockButtons >
<apex:actionRegion >
<apex:commandButton value="Save" action="{!save}" rerender="showmsg" />
<apex:commandButton value="Return to Account" action="{!cancel}" />
</apex:actionRegion>
</apex:pageBlockButtons>
</apex:pageblock>
</apex:form>
Controller
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'};
stdController.addFields(fieldnames);
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();
try
{
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){
a.Body=Attach;
a.Name=filename;
a.ParentID=fsh.id;
fshAtt.add(a);
}
}
update newFSHs;
update this.acct;
insert fshAtt;
System.debug(this.acct.ownerid);
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());
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'There was an error.. ' + e.getmessage()));
return null;
}
return null;
}
}