Activity › Forums › Salesforce® Discussions › How to upload multiple attachments from Salesforce apex? please provide me some sample code
Tagged: Attachments, Content Document, Content Version, DML Operation, Salesforce Apex, Salesforce Apex Code, Salesforce Email Attachments
-
How to upload multiple attachments from Salesforce apex? please provide me some sample code
Posted by Tanu on September 29, 2016 at 2:20 PMHi All,
I want to upload multiple attachments from apex? please provide me some sample code.
Avnish Yadav replied 4 years, 4 months ago 3 Members · 2 Replies -
2 Replies
-
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.
- [adinserter block='9']
-
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.