I am able to use a file upload to post a file to the Chatter Feed of a custom object record using the following code:
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
//Add message text to post
messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
textSegmentInput.text = 'New Document Posted by: ';
messageBodyInput.messageSegments.add(textSegmentInput);
mentionSegmentInput.id = UserInfo.getUserId();
messageBodyInput.messageSegments.add(mentionSegmentInput);
feedItemInput.body = messageBodyInput;
//Add attachment data from uploaded file
ConnectApi.NewFileAttachmentInput attachmentInput = new ConnectApi.NewFileAttachmentInput();
attachmentInput.description = 'Uploaded by: ' + UserInfo.getUserId();
attachmentInput.title = doc.name;
feedItemInput.attachment = attachmentInput;
//Set visibility of post
feedItemInput.visibility = ConnectApi.FeedItemVisibilityType.InternalUsers;
//Create binary from file upload
ConnectApi.BinaryInput binaryInput = new ConnectApi.BinaryInput(doc.Body, 'text/plain', doc.name);
ConnectApi.FeedItem originalFeedItem = ConnectApi.ChatterFeeds.postFeedItem(communityId, ConnectApi.FeedType.Record, SObjectId, feedItemInput, binaryInput);
//Redirect to order page
String orderUrl = ENV_SOBJECT_URL + sObjectId;
return new PageReference(orderUrl);
The file is displayed in the chatter feed and notes and attachments section on the custom object details page
QUESTION
I want to be able to change the File Sharing settings of the file such that only users in certain groups are able to view that file. So the owner of the Feed Item will be the record but only users in certain groups will be able to see the file.
I am currently using the Connect API to accomplish this but using SOQL would be fine as well.