You need to sign in to do that
Don't have an account?
Snehal Gaware 15
While creating record with attchment via vf, i am getting error as missing required field body error
Hi
I am creating case through vf page where i have to attach attchment to that case while creating that case record but when i hit the save button. I am geeting error as
"Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]
Error is in expression '{!Save}' in component <apex:commandButton> in page newcase_page_for_community: Class.AutoPopulateExample.save: line 64, column 1"
Please let me know if i am doing something wrong.
VF page
Apex Controller :
Please suggest.
I am creating case through vf page where i have to attach attchment to that case while creating that case record but when i hit the save button. I am geeting error as
"Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]
Error is in expression '{!Save}' in component <apex:commandButton> in page newcase_page_for_community: Class.AutoPopulateExample.save: line 64, column 1"
Please let me know if i am doing something wrong.
VF page
<apex:page standardController="Case" extensions="AutoPopulateExample"> <apex:form id="form"> <apex:pageBlock > <apex:pageBlockButtons location="top"> <apex:commandButton value="Save" action="{!Save}"/> <apex:commandButton value="Cancel" action="{!Cancel}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Basic Details" columns="2"> <apex:inputField label="Contact Name" value="{!Case.ContactId}" > <apex:actionSupport event="onchange" action="{!autoCal}" reRender="form"/> </apex:inputField> <apex:inputField label="Requestor First Name" value="{!Case.Requestor_First_Name__c}"/> <apex:inputField label="Account Name" value="{!Case.AccountId}"/> <apex:inputField label="Requestor Last Name" value="{!Case.Requestor_Last_Name__c}"/> <apex:inputField label="Status" value="{!Case.Status}"/> <apex:inputField label="Requestor Email" value="{!Case.Requester_Email__c}"/> <apex:inputField label="Subject" value="{!Case.Subject}"/> <apex:inputField label="Requestor Contact Number" value="{!Case.Requester_Contact_Number__c}"/> <apex:inputField label="Product" value="{!Case.Products__c}"/> <apex:inputField label="Business Impact" value="{!Case.Business_Impact__c}"/> <apex:inputField label="Components" value="{!Case.Componet__c}"/> <apex:inputField label="Business Urgency" value="{!Case.Business_Urgency__c}"/> <apex:inputField label="On Behalf of" value="{!Case.On_Behalf_of__c}"/> <apex:inputField label="Environment" value="{!Case.Environment__c}"/> <apex:inputField label="Description" value="{!Case.Description__c}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <apex:form > <apex:outputPanel id="theSection" rendered="true"> <apex:pageblock > <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1"> <div id="upload" class="upload"> <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/> </div> </apex:pageBlockSection> </apex:pageBlock> </apex:outputPanel> </apex:form> </apex:page>
Apex Controller :
public class AutoPopulateExample { public string fileName{get;set;} public Blob fileBody{get;set;} public Contact con{get;set;} public Case caseObject{get;set;} public Attachment attachmentObject { get { if (attachmentObject == null) attachmentObject = new Attachment(); return attachmentObject ; } set; } public ApexPages.StandardController stdCntrlr {get; set;} public AutoPopulateExample(ApexPages.StandardController controller) { con = new Contact(); caseObject = new case(); caseObject = (Case)controller.getRecord(); attachmentObject = new Attachment(); } //function is called from actionsupport event public void autoCal() { Id conid = caseObject.ContactId; // collecting contact id from visualforce page List<Contact> conLst = [select id,AccountId,FirstName,LastName,Email,Phone from contact where id=:conid]; if(conLst.isEmpty()) { return; } caseObject.Requestor_First_Name__c = conLst[0].FirstName; caseObject.AccountId = conLst[0].AccountId; caseObject.Requestor_Last_Name__c = conLst[0].LastName; caseObject.Requester_Email__c = conLst[0].Email; caseObject.Requester_Contact_Number__c = conLst[0].Phone; } public PageReference save() { // Save the Case insert caseObject; // Save the Attachment using the Case id as the parent Id System.debug('@@@@@fileBody'+fileBody); attachmentObject = new Attachment(); Integer i=0; attachmentObject .clear(); attachmentObject.Body = fileBody; attachmentObject.Name = 'Logo_'+caseObject.id+'.jpeg' ; attachmentObject.ParentId = caseObject.id; insert attachmentObject; pagereference pr = new pagereference('/'+caseObject.id); return pr; } }
Please suggest.
You are getting this error because of you have added two <apex:form> on the VF Page, Since you have placed the <apex:commandButton> on First form on clicking the save button your first form only will get submitted.
So the attachment remains null.
Replace your VF code by below code,
No changes needed on the apex class.
Let me know if you have any questions.
Regards,
Prasath
All Answers
You are getting this error because of you have added two <apex:form> on the VF Page, Since you have placed the <apex:commandButton> on First form on clicking the save button your first form only will get submitted.
So the attachment remains null.
Replace your VF code by below code,
No changes needed on the apex class.
Let me know if you have any questions.
Regards,
Prasath
i have added 2 forms to avoid following error message:
"apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute. "
Yes, We will get this error if both action component and apex:inputFile with in a form.
Thats why I have added the <apex:actionRegion> on the vf page. The Above code will work fine.
Just try and let me know if it is not working.
Mark this as a best answer if it solves your Problem.
Thank you so much. It is working as expected now when i add attachment while creating case.
But when i try to create case without adding an attchment it is showing me same error again, is there any solution for it.
Try the below code to fix that error. Mark this as a best answer if it solves your Problem.