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
SMasterSMaster 

Using Visualforce Page.. how to upload file in Notes & Attachments

Hi All,

 

In the custom object.. i want to upload a file inside Notes & Attachments using the visualforce page.. please let me know how can i do that....

Best Answer chosen by Admin (Salesforce Developers) 
Shailesh DeshpandeShailesh Deshpande

just use Trigger.New instead of Trigger.New()

All Answers

Virendra NarukaVirendra Naruka

Hi

 

You can use 

 

<apex:inputFile value="{!File}" filename="{!FileNameToAttach}" contentType="Type of File" id="inputFile" title="Type the path of the file or click the Browse button to find the file."/>

 

On your VF Page

 

on Apex class use like .......

Attachment myAttachment = new Attachment();

 

Assigned values to Attachment object's  Fields 
Assign Parent id Of Attachment = Your Custom Object Id 
and Insert the Attachment

 

 

 

SMasterSMaster

Hi virendra,

 

Unfortunately i have no idea to implement this logic... i was using totally different approach...

 

My Custom Object name is >  opportunity_proposals__c

 

 

Please provide me the code snippet for the apex and visualforce page... i'll really be thankful...

SMasterSMaster

hello Sir,

 

Based on the suggestion provided by you I am using VF page for this... and created a apex class to upload the file inside Notes & attachments...

 

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; 
    }          
}

 

 

and i have created the following visualforce page;

 

 

<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">
<apex:form >
   <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>

 

 

but its not working please confirm... where did i commited a mistake..

Virendra NarukaVirendra Naruka

Hi 

are you able to open  browse window to select file. 

 

Try contentType Like

 

<apex:inputFile value="{!FileBody}" filename="{!FileName}" contentType="{!contentType}" id="inputFile" title="Type the path of the file or click the Browse button to find the file."/>

 

In controller 

save Attachments with contentType

newAttachment.ContentType = contentTypeFile;

 

 

SMasterSMaster

Hello sir,

 

I am able to see Browse window to select file.. but after selecting the file as i click on the upload button i get the following error;

 

"

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]

 

Class.PropUpload.UploadFile: line 26, column 11 External entry point "

 

if possible please suggest the code changes.. and provide the modified code... please

 

Virendra NarukaVirendra Naruka

Please make sure that 

For 

 

myAttachment.ParentId = recId;  

 

Check recId is not null and 

recId should be  valid Sobject Id

SMasterSMaster

sir,

 

how can i know that....  i am new in this.. please suggest

Virendra NarukaVirendra Naruka

Add <apex:messages/>  Tag to your VF Page 

 

Add Following lines to Your Controller UploadFile Method 

 

 

ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'RecordId Parent Id::'+recId);
 ApexPages.addMessage(msg);
it will display recId on your Page on Upload Button
Make sure to comment Insert attachment  Statement  on controller 

 

SMasterSMaster

Perfectly done Sir... Now i am able to upload the file sucessfully inside Notes & Attachments...

 

Now.. Sir another important thing to implement in this.. on the page layout i have a custom button and after clicking on that button..a field will be updated... and the comlete record along with Notes and attachments will be freezed..... how can i achieve that... please guide..

 

 

 

Virendra NarukaVirendra Naruka

write a trigger to update the Field of custom object 

 

rest things will be as it is ...

SMasterSMaster

Sir,

 

Please provide the code to update the field with some value after attachment is saved...

 

My Custom Object Name is opportunity_proposals__c and custom field name is Proposal_Status__c with data type as text...

 

 

Virendra NarukaVirendra Naruka

trigger updateFiendsForObject on opportunity_proposals__c (before update){

 

  for(opportunity_proposals__c oppProp: Trigger.New()){

      oppProp.Proposal_Status__c = "Update Value";

  }

 

}

SMasterSMaster

Sir,

 

it is giving Compile Error: Method does not exist or incorrect signature: Trigger.New() at line 3

 

trigger updateFiendsForObject on opportunity_proposals__c (before update)
{
  for(opportunity_proposals__c oppProp: Trigger.New())
  {
      oppProp.Proposal_Status__c = 'Sent';

  }
}

SMasterSMaster

Its done sir... Trigger is working fine... can it be done on button click... i mean how i update the field on button click...

Shailesh DeshpandeShailesh Deshpande

just use Trigger.New instead of Trigger.New()

This was selected as the best answer
SMasterSMaster

Hello Sir,

 

Please suggest the solution to display all the attachments on Visualforce page...

Virendra NarukaVirendra Naruka

Hi 

 

1.Query for  all the attachment where ParentId of attachment is equal to your sObject 

2.It will give you the list of Attachment 

3.Use page BlockTable to display all the attachments  of vf Page 

 

 

 

 

 

SMasterSMaster

Hello Sir,

 

i am using the following 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 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; 
    }          
}

 

 

and i have created the following visualforce page;

 

 

<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">
<apex:form >
   <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>

 

 

 

 

please make the changes in the code that are required.. to display the uploaded attachments.. on visualforce page....