4

I want to use the "Set by Record" sharing option of Chatter files introduced in Winter '16.

As far as I understood I have to add an AfterInsert trigger on ContentDocumentLink and reset the ShareType.

But to which value? Where are they officially documented?

3 Answers 3

6

This is available now in the Winter '20 release! https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_files_sharing_set_by_record.htm

8

Set By Record can be done upon assigning

ShareType = "I", where it means:

Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library.

Other Share Types are:

V Viewer premission. The user can explicitly view but not edit the shared file.

C Collaborator permission. The user can explicitly view and edit the shared file.

Given a small piece of code for this assignment

//create an account 
Account acct = new Account(Name='TEST_ACCT');
insert acct;

ContentVersion contentVersion = new ContentVersion(
  Title = 'Test',
  PathOnClient = 'test.jpg',
  VersionData = Blob.valueOf('Test Content'),
  IsMajorVersion = true
);
insert contentVersion;    
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

//create ContentDocumentLink  record for that account
ContentDocumentLink cdl = New ContentDocumentLink(
            LinkedEntityId = acct.id, ContentDocumentId = documents[0].Id, shareType = 'I');
insert cdl;

Also, created a file with Set By Record at my org.

Chatter set by record

Workbench query

Workbench set by record

For more information, refer ContentDocumentLink

5
  • Wow. That was fast. Thanks for the detailed answer. Commented Jan 23, 2017 at 16:50
  • Can I do this in a BeforeInsert Trigger or must it be an AfterInsert trigger? Commented Jan 23, 2017 at 17:04
  • I think you can do both, let me know how it works Commented Jan 23, 2017 at 17:18
  • Still confused. When I insert a ContentPost FeedItem does Salesforce automatically insert ContentDocumentLinks? Or do I have to do it manually Commented Jan 24, 2017 at 10:25
  • Okay as per my understanding, shareType = 'I' means if the user has read access to the parent record notes also will have read access only and if it has write access then he can update note content as well. So if the user has write access on note will he be able to share the record with any user Commented Jun 11, 2020 at 13:36
1

Adding onto @Sanatanu answer because i struggled getting this right initially. This needs to be ran After Insert on a Content Version Trigger. After grabbing all of the content version contentDocId's i threw it into this method. i would suggest switching this to @future and dropping the return, but that is up to you.

public static final String SNOTE    = 'SNOTE';
public static List<ContentDocumentLink> shareByRecord(Set<Id> contentDocIdSet) {
    List<ContentDocumentLink> documentLinkList  = new List<ContentDocumentLink>();
    List<ContentDocumentLink> documents = [SELECT Id, ContentDocument.FileType
                                            FROM ContentDocumentLink 
                                            WHERE ContentDocumentId =: contentDocIdSet 
                                            AND LinkedEntityId <> null];                                        
    String fileType;
    For(ContentDocumentLink doc:documents){
        fileType    = doc.Contentdocument.FileType;
        if(SNOTE.equalsIgnoreCase(fileType)) {
            documentLinkList.add( New ContentDocumentLink(id = doc.id,
                                                        shareType = 'I'));
        }
    }                               
    return documentLinkList;
}

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .