+ Start a Discussion
Harjeet Singh 28Harjeet Singh 28 

Insert failed. REQUIRED_FIELD_MISSING, Required fields are missing: [Version Data]: [Version Data]

Hi All,

I am working on a requiremenmt where I need to present users with an input form where they can fill up the details and then can upload files from their machine.
I have created the input forms but facing some issue for file upload functionality.I have a custom object and added the related list "Files".If user upload the document and save the record then record will be sdaved with their inputs and uploaded file can be seen under Files related list.
Actual problem comes up when user doesn't select/attach any file and directly click on save button then I am getting error "Insert failed. REQUIRED_FIELD_MISSING, Required fields are missing: [Version Data]: [Version Data]".Ideally I dont want user to necessarily upload a file.

Below is my VF Page
<!--<apex:page Controller="GrabSurveyForDriverController" sidebar="false" lightningStylesheets="true">-->
<apex:page standardController="Grab_Driver_Survey__c" extensions="GrabSurveyForDriverController" sidebar="false" lightningStylesheets="true">
    
    
    <apex:form >
        
        <apex:pageBlock mode="edit" >
            <apex:pageBlockSection columns="1" collapsible="false" title="Driver Information"  >
                <apex:inputField label="Name" value="{!survey.Name__c}" required="true"/>
                <apex:inputField label="Mobile" value="{!survey.Mobile__c}" required="true" />
                <apex:inputField label="Email" value="{!survey.Email__c}" required="true" />
            </apex:pageBlockSection>
            
            
            <apex:pageBlockSection columns="2" collapsible="false" title="Fact Finding">
                
                <!--<apex:pageBlockTable var="survey" value="{!surveyList}">-->
                <apex:inputField label="Reason for joining" value="{!survey.Reason_for_joining__c}"/>
                <apex:inputField label="What are you previously or currently working as?" value="{!survey.Previously_or_currently_working_as__c}" />
                <apex:inputField label="How long have you been working there?" value="{!survey.How_long_have_you_been_working_there__c}" />
                <apex:inputField label="Reason for leaving" value="{!survey.Reason_for_leaving__c}" />
                <apex:inputField label="Previous or current salary range (per annum)" value="{!survey.Previous_or_current_salary_range_annum__c}" />
                <apex:inputField label="How much do you expect to earn by driving (weekly)" value="{!survey.How_much_you_expect_to_earn_weekly__c}" />
                <apex:inputField label="What car will you be using? " value="{!survey.What_car_will_you_be_using__c}" />
                <apex:inputField label="How many hours are you able to drive in a day? " value="{!survey.How_many_hours_you_able_to_drive_a_day__c}" />
            </apex:pageBlockSection>
            
          
 <apex:pageBlockSection columns="2" collapsible="false" title="Interview Questions" id="pbToRerender" >
                
                <apex:inputField label="What is your past records? (Demerit Points) " value="{!survey.What_is_your_past_driving_records__c}"/>
                <apex:actionRegion>
                <apex:inputField label="Any Criminal Records?(Yes/No)" value="{!survey.Any_Criminal_Records__c}" id="id1" >
                    <apex:actionSupport event="onchange" rerender="pbToRerender"/>
                </apex:inputField>
                </apex:actionRegion>
                <!--<apex:inputField label="Reasons associated with Criminal Records" value="{!survey.Reasons_associated_with_Criminal_Records__c}" />-->
                <apex:inputField label="Are you currently employed or any other source of income (What job)" value="{!survey.Are_you_currently_employed_or_other_job__c}" />
                
            </apex:pageBlockSection>
             <apex:inputFile value="{!file}" styleclass="slds-file-selector__input slds-assistive-text"/>
            <apex:pageBlockButtons >
                <apex:actionRegion >
                <apex:commandButton value="Save" action="{!doFullSave}"/>
                <apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/>
            </apex:actionRegion>
            </apex:pageBlockButtons> 
       
        </apex:pageBlock>
    </apex:form>
</apex:page>

Below is my controller:
public class GrabSurveyForDriverController{

    public Grab_Driver_Survey__c survey{get;set;}
    public String newSurveyList {get;set;}
    public String newSurveyListSecond {get;set;}
    public String newSurveyListThird {get;set;}
    public String newSurveyListFourth {get;set;}
    public String newSurveyListFifth {get;set;}
    public blob file { get; set; }
     public string fileName{get;set;}

    public GrabSurveyForDriverController(ApexPages.StandardController controller){
         survey=new Grab_Driver_Survey__c();
         //ContentVersion v = new ContentVersion();
        
        system.debug('message3>>'+survey );
    }
    
    public PageReference doFullSave(){
        system.debug('message1>>'+survey );
        
       insert survey; 

       ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
        conVer.PathOnClient = 'file'; // The files name, extension is very important here which will help the file in preview.
        conVer.Title = 'fileName '; // Display name of the files
        conVer.VersionData = file; // converting your binary string to Blob
        insert conVer;
        
        Id conDoc = [SELECT Id,ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
      
        //Create ContentDocumentLink
        ContentDocumentLink cDe = new ContentDocumentLink();
        cDe.ContentDocumentId = conDoc;
        cDe.LinkedEntityId =survey.Id; // you can use objectId,GroupId etc
        cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
        cDe.Visibility = 'AllUsers';
      
        insert cDe;
        
        PageReference pageRef = new PageReference('/'+survey.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }
    public PageReference doCancel(){
        PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
            return pageRef;
    }
    
    
}

Summary of my problem statement:
1. When user enters the inputs in the form and upload the file and hit Save button then the record is getting created with the uploaded files under Files realted list-This is absolutely working fine

2. When an user enters the inputs in the form and doesn't select/upload any files and hit Save button then I am getting error "Insert failed. REQUIRED_FIELD_MISSING, Required fields are missing: [Version Data]: [Version Data]".Ideally it should also allows an user to save the record even if they don't want to upload any files

Kindly help

Many thanks in advance