You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Add attachment button on Visualforce page
I am working on a visualforce page that allows a user to uppdate an opportunity and opportunityLineItems and give them the option of attaching a file. The problem I am running into is that if you don't attach a file I get the error below. The user needs to be able to add or not add the attachment. Any suggestions?
This is the error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [File Name, Body]: [File Name, Body]
Error is in expression '{!saveIt}' in component <apex:commandButton> in page orderonhand: Class.OrderOnHandClass.saveIt: line 30, column 1
Class.OrderOnHandClass.saveIt: line 30, column 1
This is the Visualforce Page:
This is the error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [File Name, Body]: [File Name, Body]
Error is in expression '{!saveIt}' in component <apex:commandButton> in page orderonhand: Class.OrderOnHandClass.saveIt: line 30, column 1
Class.OrderOnHandClass.saveIt: line 30, column 1
This is the Visualforce Page:
<apex:page standardController="Opportunity" extensions="OrderOnHandClass"> <apex:form > <apex:pageBlock > <apex:pageBlockSection title="Opportunity Details"> <apex:inputCheckbox value="{!opportunity.Order_on_Hand__c}"/> <apex:inputField value="{!Opportunity.PO_Number__c}"/> <apex:inputField value="{!Opportunity.Order_Ship_To_Location__c}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Opportunity Product Details"> <apex:pageBlockTable var="OLI" value="{!OLIs}" id="newProduct"> <apex:column value="{!OLI.ProductCode}"/> <apex:column headerValue="Quantity"> <apex:inputfield value="{!OLI.Quantity}"/> </apex:column> <apex:column headerValue="Order Quantity if Different"> <apex:inputfield value="{!OLI.Order_Quantity_if_Different__c}"/> </apex:column> <apex:column headerValue="Approved Price"> <apex:outputfield value="{!OLI.Approved_Price__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!saveIt}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Attachment" > Attach PO: <apex:inputfile value="{!a.body}" filename="{!a.name}"></apex:inputfile> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>This is the Class:
public class OrderOnHandClass { public ApexPages.StandardController sc; public Opportunity Opp {get;set;} public List<OpportunityLineItem> OLIlist2 {get ;set;} public Attachment a {get;set;} public OrderOnHandClass(ApexPages.StandardController sc) { this.Opp = (Opportunity)sc.getRecord(); OLIlist2 = [Select Name, ID,ProductCode, Quantity,Approved_Price__c, OpportunityId,Order_Quantity_if_Different__c FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id]; this.a = new Attachment(); } public List<OpportunityLineItem> getOLIs() { return OLIlist2; } public PageReference saveIt() { opp.stageName = 'Order Received'; update Opp; update OLIlist2; if(a.Id == Null){ a.parentId = opp.Id; insert a; }else{ Return Null; } String p = ApexPages.currentPage().getParameters().get('param'); if(p == null){ PageReference pageRef = new PageReference('https://cs41.salesforce.com/'+opp.Id); return pageRef; } else{ return null; } } }
All Answers