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

  • Gourav

    Member
    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.

    http://forceguru.blogspot.in/2012/12/uploading-multiple-attachments-into.html

    Hope this will help you.

    Thanks.

  • Avnish Yadav

    Member
    September 29, 2018 at 8:28 pm

    Hello,
    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:form>
    <apex:inputfile value="{!file}"></apex:inputfile>
    <apex:commandbutton action="{!upload}" value="Upload"></apex:commandbutton>
    </apex:form>
    </apex:page>

    Class:
    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('/' + v.id);
    }
    }

    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.
    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos