function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash RaviAvinash Ravi 

Need to Upload a file while creating a record on VF

Hi All,

I have a custom object called Leap_Weekly_Curriculum__c and I need a VF page that will help me input values into it and also upload a file which should get added to the attachments related list of the same record. Please help.
KaranrajKaranraj
Avinash - Try the below code

Visualforce Page:
<apex:page standardController="Leap_Weekly_Curriculum__c" extensions="PropUpload">
<apex:form >
<apex:inputfield value="{!Leap_Weekly_Curriculum__c.Name}" />
<!-- Include other fields which you want to have it the visualforce page -->
<div id="upload" class="upload">
<table>
<tr>
  <td><apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/></td> 
  <td><apex:commandButton value="Upload Attachment" action="{!uploadFile}"  styleClass="upload-button"/></td>
 </tr>
</table>
</div>     
</apex:form>
</apex:page>
Apex Class:
public class PropUpload  {  
    public Id recId {    get;set;    }  
      
    public PropUpload(ApexPages.StandardController​ ctlr)  
    {  
       recId = ctlr.getRecord().Id;       
    }  
      
    public string fileName   
    {    get;set;    }  
      
    public Blob fileBody   
    {    get;set;    }  
    
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
          Attachment myAttachment  = new Attachmen​t();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
             PageReference page = ApexPages.curren​tPage();
             page.setRedirect(true);
             return page;              
        }  
        return null; 
    }          
}


 
Avinash RaviAvinash Ravi
Hi Karan,

Thanks for responding.

But I'm getting this error now :  [Error] Error: Compile Error: Invalid type: attachmen​t at line 33 column 41

Please help out!

Thanks,
Avinash
KaranrajKaranraj
Thats problem in copy and pasting the code into your org. Try the below code
 
public class PropUpload  {  
    public Id recId {    get;set;    }  
     
    public PropUpload(ApexPages.StandardController ctlr)  
    {  
       recId = ctlr.getRecord().Id;       
    }  
      
    public string fileName   
    {    get;set;    }  
      
    public Blob fileBody   
    {    get;set;    }  
    
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
          Attachment myAttachment  = new Attachment();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
             PageReference page = ApexPages.currentPage();
             page.setRedirect(true);
             return page;              
        }  
        return null; 
    }          
}

If you re still getting the error message then just delete that error line and retype the same line again(don't copy and paste). You will also get another error message in the Apex.currentPage(); again repeat the same, delete that error line and retype once again.To identify the sapace between the text in the code, open the apex class in the developer console, it will identify the space inbetween letters and show in red dot color. 

 
Avinash RaviAvinash Ravi
Hi Karan,

This is what I'm getting now.. guess it isn't saving the curriculum record first up.

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
Error is in expression '{!uploadFile}' in component <apex:commandButton> in page leapcurriculum: Class.PropUpload.UploadFile: line 35, column 1
An unexpected error has occurred. Your development organization has been notified.
Avinash RaviAvinash Ravi
Line 35 is insert Attachment;

Now I tried changing this myAttachment.Parent.Id = recId; and this is the error I'm getting

Attempt to de-reference a null object
Error is in expression '{!uploadFile}' in component <apex:commandButton> in page leapcurriculum: Class.PropUpload.UploadFile: line 33, column 1
An unexpected error has occurred. Your development organization has been notified.
TanDTanD
Did you solve the issue? I am getting similar error.
Thomas Reinman 18Thomas Reinman 18
I think the issue is that this is a new record so the Id doesn't exist yet, hence the null object error. Working on a similar problem now so I'll update the thread if I find a solution.