Activity Forums Salesforce® Discussions How to upload multiple attachments from Salesforce apex? please provide me some sample code

  • How to upload multiple attachments from Salesforce apex? please provide me some sample code

  • Tanu

    September 29, 2016 at 2:20 pm

    Hi All,

    I want to upload multiple attachments from apex? please provide me some sample code.

  • Gourav

    October 3, 2016 at 12:56 pm

    Hi Tanu,

    You can create an VF page to insert multiple attachments. Try this link to get more information according your requirement.

    Hope this will help you.


  • Avnish Yadav

    September 29, 2018 at 8:28 pm

    ContentDocument object does not allow insert DML operation in Salesforce, so we can upload it through the ContentVersion object, without ContentDocumentId. After DML on ContentVersion a new version of ContentDocument will be created for us in salesforce.

    The easiest way to do this is using inputFile and assign it to ContentVersion instance. Like this:

    <apex:page controller="ContentController">
    <apex:inputfile value="{!file}"></apex:inputfile>
    <apex:commandbutton action="{!upload}" value="Upload"></apex:commandbutton>

    public class ContentController {
    public blob file { get; set; }`

    public PageReference upload() {
    ContentVersion v = new ContentVersion();
    v.versionData = file;
    v.title = 'testing upload';
    v.pathOnClient ='/somepath.txt';
    insert v;
    return new PageReference('/' +;

    If you want to share the ContentVersion file then after insert DML query the ContentDocumentId from inserted ContentVersion and use ContentDocumentLink to create association between Record and ContentVersion uploaded file.

  • File Zipo

    December 29, 2021 at 8:35 am

    Hi,You can try File ZIPO, a native appexchange application that can help you to Add Multiple Salesforce files & attachments of any size or volume from External Cloud to Salesforce directly.
    For more info follow the given link -

Viewing 1 - 4 of 4 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos