When uploading a new FeedItem we want to share it to a specific community. First thing we need to do is share the FeedItem to a community by sharing it to the User. As the documentation states: Only feed items with a Group or User parent can set a NetworkId or a null value for NetworkScope.
NetworkId—The ID of the community in which the FeedItem is available. If left empty, the feed item is only available in the default community.
FeedItemTrigger Trigger
trigger FeedItemTrigger on FeedItem (after insert, before insert) { fflib_SObjectDomain.triggerHandler(App_Domain_FeedItem.class); }
FeedItemTrigger Domain Class
public class App_Domain_FeedItem extends fflib_SObjectDomain { public override void onBeforeInsert(){ List<ContentDocumentLink> contentDocumentLinksList = new List<ContentDocumentLink>(); for(FeedItem record : (List<FeedItem>) Records){ record.Body = record.ParentId; record.NetworkScope = {NetworkId}; record.ParentId = UserInfo.getUserId(); record.Visibility='AllUsers'; } } }
Now that the file is shared with the user we can give View sharing back to the original object using ContentDocumentLink by using the RelatedRecordId
public class App_Domain_FeedItem extends fflib_SObjectDomain { public override void onAfterInsert(){ List<ContentDocumentLink> contentDocumentLinksList = new List<ContentDocumentLink>(); List<Documents__c> bpDocumentsList = new List<Documents__c>(); Map<Id, Id> feedItemToRelatedRecordIdMap = new Map<Id, Id>(); for(FeedItem record : (List<FeedItem>) Records){ feedItemToRelatedRecordIdMap.put(record.Id, record.RelatedRecordId); } Map<Id, Id> mapOfRelatedRecordContentVersionMap = new Map<Id, Id>(); List<ContentVersion> contentDocumentVersions = [Select Id, ContentDocumentId from ContentVersion where id IN :feedItemToRelatedRecordIdMap.values()]; for (ContentVersion contentDocumentVersion : contentDocumentVersions){ mapOfRelatedRecordContentVersionMap.put(contentDocumentVersion.Id, contentDocumentVersion.ContentDocumentId); } for(FeedItem record : (List<FeedItem>) Records){ String bpDocumentId = record.Body; Id relatedRecordId = feedItemToRelatedRecordIdMap.get(record.Id); Id contentDocumentId = null; if (mapOfRelatedRecordContentVersionMap.containsKey(relatedRecordId)){ contentDocumentId = mapOfRelatedRecordContentVersionMap.get(relatedRecordId); } if (contentDocumentId!=null){ ContentDocumentLink cdl = new ContentDocumentLink(LinkedEntityId = bpDocumentId , ContentDocumentId=contentDocumentId , shareType = 'V'); contentDocumentLinksList.add(cdl); } Documents__c updateDocument = new Documents__c(Id=bpDocumentId); updateDocument.Document_Status__c='Downloaded'; String formatDownloadUrl = '/CommunityApi/sfc/servlet.shepherd/version/download/{0}?asPdf=false&operationContext=CHATTER'; updateDocument.Download_Url__c = String.format(formatDownloadUrl, new List<String>{contentDocumentId}); updateDocument.File_Id__c = contentDocumentId; updateDocument.File_Name__c = record.Title; updateDocument.File_Size__c = record.ContentSize; updateDocument.File_Type__c = record.ContentType!=null ? mimeTypeMap.containsKey(record.ContentType.toLowerCase()) ? mimeTypeMap.get(record.ContentType.toLowerCase()) : record.ContentType : record.ContentType; updateDocument.File_Permission__c='R'; bpDocumentsList.add(updateDocument); } if (!contentDocumentLinksList.isEmpty()){ insert contentDocumentLinksList; } if (!bpDocumentsList.isEmpty()){ update bpDocumentsList; } } }
Now the Feeditem is shared both to the user and the object. It can be downloaded by the logged in user.