Find your content:

Search form

You are here

Different attachment in same VF page


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.

Attribution to: AnuRaj

Possible Suggestion/Solution #1

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="{!}" />
            <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="{!}" />
            <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="{!}" />

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.

Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

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


<apex:inputFile value="{!doc.body}" filename="{!}" 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;

Attribution to: SFBlogForce
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content