There are different ways you can use salesforce to attach a file to an SObject using Chatter.
- Apex Code
- Chatter REST Api
public static void createAttachmentFeed(List<Attachment> attachments){ for (Attachment attachment : attachments){ // ContentVersion is how you upload a file! ContentVersion version = new ContentVersion(); version.Title=attachment.Name; version.PathOnClient = '\\' + attachment.Name; version.VersionData = attachment.Body; version.NetWorkId = brightPlanWebCustomSettings.Community_Id__c; insert version; // After you insert the ContentVersion object, a base 'ContentDocument' is established // The ID of the ContentDocument is what you need to attach the file to the Chatter post. version = [SELECT ID,ContentDocumentId FROM ContentVersion WHERE ID=:version.id]; // Form a basic post attached to our own feed. ConnectApi.FeedItemInput feedItem = new ConnectApi.FeedItemInput(); feedItem.subjectId = attachment.ParentId; // This can also be an objectID to post the file to. // Now connect the feeditem to our already uploaded file. feedItem.capabilities = new ConnectAPI.FeedElementCapabilitiesInput(); feedItem.capabilities.files = new ConnectAPI.FilesCapabilityInput(); feedItem.capabilities.files.items = new List<ConnectAPI.FileIdInput>(); ConnectAPI.FileIdInput attachFile = new ConnectAPI.FileIDInput(); //**** Here is where we attach the specific file to the post! attachFile.id = version.contentDocumentid; feedItem.capabilities.files.items.add(attachFile); // Execute the posting ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(brightPlanWebCustomSettings.Community_Id__c, feedItem); } }
Some limitations using this is using Blob for version.VersionData = attachment.Body; will run into Apex String length exceeds maximum: 6000000.
Chatter Rest API
HEADER
Authorization Bearer {sessionToken}
Content-Type multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Format is as follows:
{salesforceOrgUrl}/services/data/v35.0/connect/communities/{salesforceCommunityId}chatter/feed-elements/batch
The subjectId is the SObject Id to which you want to link the file, is this case we use our Case Id.
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq Content-Type: application/json; charset=UTF-8 Content-Disposition: form-data; name="json" { "inputs": [ { "binaryPartNames": [ "file1" ], "richInput": { "subjectId": "50063000003YlSe", "capabilities": { "content": { "title": "file1.pdf" } }, "feedElementType": "FeedItem" } }, { "binaryPartNames": [ "file2" ], "richInput": { "subjectId": "50063000003YlSe", "capabilities": { "content": { "title": "file2.pdf" } }, "feedElementType": "FeedItem" } } ] } --a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq Content-Type: application/octet-stream; charset=ISO-8859-1 Content-Disposition: form-data; name="file1"; filename="file1.pdf" ...contents of file1.pdf... --a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq Content-Disposition: form-data; name="file2"; filename="file2.pdf" Content-Type: application/octet-stream; charset=ISO-8859-1 ...contents of file2.pdf... --a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--
Benefits you can batch upload files without hitting an Apex limits.
Both the Chatter and Apex solutions can be used by Community Users to upload files.
Can you please share visual force pages??
Hi- Did you hit any limitations when uploading a file using custom API.