+ Start a Discussion
Harjeet Singh 28Harjeet Singh 28 

How to upload a file and associate with the record in Files related List

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 an object and added the related list "Files" to the object but not able to link the uploaded file to the record.

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:pageBlockButtons >
                <apex:actionRegion>
                <apex:commandButton value="Save" action="{!doFullSave}"/>
                <apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/>
            </apex:actionRegion>
            </apex:pageBlockButtons>  
           <!--<apex:inputFile value="{!file}" filename="{!fileName}"/>
    <apex:commandbutton action="{!upload}" value="Upload" />  -->
        </apex:pageBlock>
        <apex:inputFile value="{!file}"/>
    <apex:commandbutton action="{!upload}" value="Upload" />
       
        
    </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; 
        system.debug('message2>>'+survey );
        PageReference pageRef = new PageReference('/'+survey.Id);
            pageRef.setRedirect(true);
        return pageRef;
    }
    public PageReference doCancel(){
        PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
            return pageRef;
    }
    public void upload() {
        system.debug('message21>>'+file );
      

        ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
        conVer.PathOnClient = 'ionicLogo.png'; // The files name, extension is very important here which will help the file in preview.
        conVer.Title = 'Proposal '; // Display name of the files
        conVer.VersionData = file; // converting your binary string to Blob
        insert conVer;
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;

        //Create ContentDocumentLink
        ContentDocumentLink cDe = new ContentDocumentLink();
        cDe.ContentDocumentId = conDoc;
        cDe.LinkedEntityId = survey.Id; 
        cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
        cDe.Visibility = 'InternalUsers';
        insert cDe;
        
    }
    
}

Any help would be greatly appreciated

Many thanks in advance

Thanks & Regards,
Harjeet​​​​​​​​​​​​​​