I have a VF page which has a opp stage field and if the stage is 70-Won then i am rerendering a panel with which accepts the file.
Interestingly the very first time when the output panel is rendered and when we upload the file, the file is always null. The code captures the exception and displays the error message and when we try upload the file again this works.
I am thinking its got to do something with the initialization of Attachment. but not sure how to correct it.
<apex:page standardController="Opportunity" extensions="CVFE_ChangeStage">
<apex:sectionHeader title="Opportunity" subtitle="{!opp.name}"/>
<apex:pageMessages id="msg"/>
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:inputField value="{!opp.stagename}" >
<apex:actionSupport event="onchange" rerender="FieldsToDisplay" />
</apex:inputfield>
</apex:pageblockSection>
<apex:outputPanel id="FieldsToDisplay" >
<apex:pageblockSection rendered="{!opp.stagename=='70-Won'}" id="pbWon" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Add Contract" />
<apex:inputFile value="{!file.body}" filename="{!fileName}" />
</apex:pageBlockSectionItem>
<apex:inputField value="{!opp.Opp_Start_Date__c}" required="true"/>
<apex:inputField value="{!opp.Annual_Agreement__c}" />
</apex:pageblockSection>
<apex:pageblockSection rendered="{!opp.stagename=='90-Lost'}" id="lost" columns="1">
<apex:inputField value="{!opp.Reason_Won_Lost__c}" />
<apex:inputField value="{!opp.Future_Opportunity__c}" />
<apex:inputField value="{!opp.Outreach_Handling__c}" />
</apex:pageblockSection>
</apex:outputPanel>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saves}" />
<apex:commandButton value="Cancel" action="{!cancel}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class
public class CVFE_ChangeStage {
public opportunity opp {get;set;}
public Attachment file{get;set;}
public String filename{get;set;}
public ApexPages.StandardController sc;
public CVFE_ChangeStage(ApexPages.StandardController sc)
{
file = new Attachment();
List<String> fieldnames = new list<String> { 'name' ,'stagename','Annual_Agreement__c','Is_Status_changed_by_VF__c','Opp_Start_Date__c' };
if (!Test.isRunningTest())
{
sc.addFields(fieldnames);
}
opp = (opportunity) sc.getRecord();
}
Public Attachment getfile()
{
file = new Attachment();
return file;
}
Public pageReference createfile()
{
file = new Attachment();
return null;
}
public pagereference cancel()
{
return new pageReference('/'+opp.id);
}
public pageReference saves()
{
Savepoint sp = Database.setSavepoint();
if(opp.stagename=='70-Won')
{
System.debug('File ' + file.body);
if(file.body != null)
{
System.debug('File is not null');
file.name = 'Contract : '+ filename;
file.parentId = opp.Id;
try {
insert file;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Contract'));
return null;
} finally {
file = new Attachment();
}
}
else
{
System.debug('File is null');
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Attach the Contract'));
return null;
}
opp.Is_Status_changed_by_VF__c = true;
}
else if(opp.stagename=='90-Lost')
{
opp.Is_Status_changed_by_VF__c = true;
}
else
opp.Is_Status_changed_by_VF__c = false;
try
{
upsert opp;
}catch(exception e)
{
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getmessage()));
return null;
}
return new pageReference('/'+opp.id);
}
}