Dec 22, 2012

Uploading Multiple Attachments into Salesforce - Simple Code

Okay, so here is the simple code to upload multiple attachments into Salesforce. Please note that I've used standard controller ("Account" in the code) so you need to pass the record Id in parameter. You can also change this according to your need as code is dynamic.

So first option allow you to select how many files you want to upload. Say, you have selected "5", then it will give you 5 options to select files and once you click on "Upload" it will upload the files as attachments to the associated record.

Visualforce Page Code:

<apex:page standardController="Account" extensions="MultipleUploadController">
        <apex:pageBlock title="Upload Multiple Attachment to Object">
                <apex:commandButton value="Upload"  action="{!SaveAttachments}"/>
            <apex:pageMessages id="MSG"/>
            <apex:actionFunction name="ChangeCount" action="{!ChangeCount}"/>
                    <apex:outputLabel value="How many files you want to upload?"/>
                    <apex:selectList onchange="ChangeCount() ;" multiselect="false" size="1" value="{!FileCount}">
                        <apex:selectOption itemLabel="--None--" itemValue=""/>
                        <apex:selectOptions value="{!filesCountList}"/>
            <apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != '', true , false)}">
                <apex:repeat value="{!allFileList}" var="AFL">
                    <apex:inputfile value="{!AFL.body}" filename="{!AFL.Name}" />

Apex Code:

public class MultipleUploadController
    //Picklist of tnteger values to hold file count
    public List<SelectOption> filesCountList {get; set;}
    //Selected count
    public String FileCount {get; set;}
    public List<Attachment> allFileList {get; set;}
    public MultipleUploadController(ApexPages.StandardController controller)
        filesCountList = new List<SelectOption>() ;
        FileCount = '' ;
        allFileList = new List<Attachment>() ;
        //Adding values count list - you can change this according to your need
        for(Integer i = 1 ; i < 11 ; i++)
            filesCountList.add(new SelectOption(''+i , ''+i)) ;
    public Pagereference SaveAttachments()
        String accId = System.currentPagereference().getParameters().get('id');
        if(accId == null || accId == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No record is associated. Please pass record Id in parameter.'));
        if(FileCount == null || FileCount == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to upload.'));

        List<Attachment> listToInsert = new List<Attachment>() ;
        //Attachment a = new Attachment(parentId = accid,, body = myfile.body);
        for(Attachment a: allFileList)
            if( != '' && != '' && a.body != null)
                listToInsert.add(new Attachment(parentId = accId, name =, body = a.body)) ;
        //Inserting attachments
        if(listToInsert.size() > 0)
            insert listToInsert ;
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ' file(s) are uploaded successfully'));
            FileCount = '' ;
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select at-least one file'));
        return null;
    public PageReference ChangeCount()
        allFileList.clear() ;
        //Adding multiple attachments instance
        for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)
            allFileList.add(new Attachment()) ;
        return null ;

You can enhance the code if you like, as I've provided the basic code which is re-usable. Also you can change the object passed in standard controller and code will handle the rest. URL should have ID else you will get error on UI :-)

Say, I've saved my VFP code as "MultipleUpload", so my URL will be https://...../apex/MultipleUpload?id=XXXXXXXXXXXXXXX (account id in this code).

Happy Coding.


  1. Thanks so much for this sample. I am pre-populating number of files base on value in my custom object. Working very well. This code save lot of time for me.

  2. Hi Ankit,

    Thanks so much for your sample programs.

    Please add the one more condition in MultipleUploadController class at line number 56.
    In this case it will throws error.
    Case : let us assume A user select the "2" from the pick list, it is working fine.But if A user select again "None" then it will throws the following error.

    System.NullPointerException: Argument 1 cannot be null
    Error is in expression '{!ChangeCount}' in component in page multiplefilesupload

    Class.MultipleUploadController.ChangeCount: line 57, column 1

    if(FileCount != Null)
    for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)
    allFileList.add(new Attachment()) ;

  3. Is there a way to restrict or check the attachment using ContentType field in attachment object?

  4. I am getting the following error when trying upload file with 2MB (during testing) Any suggestions on how I can use "transient" with this example
    Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 1,287.984KB

    1. Even I am getting this error, please help.

    2. Hi, I think you should add on line# 38. So the list is empty, it will drop the view state size.


  5. While uploading the document i am getting the following error. System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
    Error is in expression '{!SaveAttachments}' in page multiupload

    Class.MultipleUploadController.SaveAttachments: line 42, column 1. How can i solve that ?

    1. Pass account id in the URL and then attache the file and upload it.

  6. Hi - This code has helped me a lot, thank you. Would you have a test class so I can migrate it to production, I am struggling to write one that tests enough lines. Kind regards Ciaran Hayden

  7. Hi Ankit,
    It's all working fine . But when i am going to insert 8-10 attachments i am getting " view state size limit (135KB) exceeded" So how can i over come this error and insert max number of attachments required.

    1. Yes Ankit,

      Even I am getting this error please suggest

  8. How can I use this code to either create a new account with attachments or add the files to an existing account?

  9. I'm sorry, what part of the code does the URL get inserted to?

    I am getting the same error as above.

    System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
    Error is in expression '{!SaveAttachments}' in page multiupload

  10. This comment has been removed by the author.

  11. this code helps me so much while creating new case with multiple attachments.

  12. Thanks so much for this sample Code..... :)

  13. How multiple attachments will work for Add attachment button which will add one more choose file?

  14. Thanks for sharing this great information I am impressed by the information that you have on this blog. Same as your blog i found another one Oracle APEX . Actually I was looking for the same information on internet for Oracle APEX and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject, you can learn more aboutOracle APEX . By attending Oracle APEX Training.