Hi i have a VF page with 5 input File attachment. I want 5 different attachment on the each of them. eg: Their are 2 more fields. 1. Name, 2. Job, 3. Resume . So like this their are 5 fields. If 1st Name = Anu, Job = SFDC dev , resume = attachment1 2nd Name = ram, job = SFDC dev, resume = attachment2 etc. Their is no field called attachment in object. I need to attach the attachment to the record we create. Is it possible. Please help me to solve this issue.


You can use the apex:inputFile component multiple times, so long as you have multiple Attachment objects in your state to bind to. Also notice that I have used the transient keyword, as you do not want to blow the viewstate governor. Assuming you where to return the user to the same page, which in my example I am not, but is good practice anyway.

public with sharing class MultipleAttachmentsController 
    public Test__c record { get; set; }
    public transient Attachment attachment1 { get; set; }
    public transient Attachment attachment2 { get; set; }
    public transient Attachment attachment3 { get; set; }

    public MultipleAttachmentsController()
        // Record and Attachments to be inserted
        record = new Test__c();
        attachment1 = new Attachment();
        attachment2 = new Attachment();
        attachment3 = new Attachment();

    public PageReference createRecordAndAttachments()
        // Insert the record
        insert record;

        // Insert Attachments;
        attachment1.ParentId = record.Id;
        attachment2.ParentId = record.Id;
        attachment3.ParentId = record.Id;
        insert new List<Attachment> { attachment1, attachment2, attachment3 };

        // View the record
        return new PageReference('/'+record.Id);

This is the page code...

<apex:page controller="MultipleAttachmentsController">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockbuttons >
                <apex:commandbutton action="{!createRecordAndAttachments}" value="Insert with Attachments" />
            <apex:pageblocksection title="Attachment 1 and Fields">
                <apex:inputField value="{!record.Name1__c}" />
                <apex:inputField value="{!record.Job1__c}" />
                <apex:inputFile value="{!attachment1.body}" filename="{!attachment1.name}" />
            <apex:pageblocksection title="Attachment 2 and Fields">
                <apex:inputField value="{!record.Name2__c}" />
                <apex:inputField value="{!record.Job2__c}" />
                <apex:inputFile value="{!attachment2.body}" filename="{!attachment2.name}" />
            <apex:pageblocksection title="Attachment 3 and Fields">
                <apex:inputField value="{!record.Name3__c}" />
                <apex:inputField value="{!record.Job3__c}" />
                <apex:inputFile value="{!attachment3.body}" filename="{!attachment3.name}" />

The resulting page looks like this...

enter image description here

And once saved the controlled redirects to the view page that looks like this...

enter image description here

NOTE: You could consider using a list and an apex:repeat to optimise this implementation a bit further. This would require a bit more code in your controller. So long as you have a fixed number of attachments and sets of fields, the above while more verbose is probably good enough.


you can insert uploaded document into "Notes & Attachments" related list to your custom object.


<apex:inputFile value="{!doc.body}" filename="{!doc.name}" contentType="{!contentType}" size="1"/>


public class NewAttachemnt{ 
public Attachment doc {get; set;} 
public String contentType {get; set;} 
public NewAttachemnt() {  
    doc = new Attachment(); 
public void save attachment() {
  doc.parentId=AccountObj.Id;  // In your case custom object
  doc.contentType = 'pdf'; // set accourding to uploded document
  insert doc;
  • i need more than 5 attachment for different records from a VF page. for 1 attachment it is working for me.
    – AnuRaj
    Nov 29 '12 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.