create(), delete(), describeSObjects(), query(), retrieve(), update()
Field | Details |
---|---|
ContentDocumentId |
|
LinkedEntityId |
|
ShareType |
|
Visibility |
The visibility setting on ContentDocumentLink determines a file’s visibility on a record post. When a file has multiple references posted in a feed, the file’s visibility is determined by the most visible setting. |
Use this object to query the locations where a file is shared or query which files are linked to a particular location. For example, the following query returns a particular document shared with a Chatter group:
SELECT ContentDocument.title FROM ContentDocumentLink WHERE ContentDocumentId = '069D00000000so2' AND LinkedEntityId = '0D5000000089123'
The ContentDocumentLink object supports triggers before and after these operations: insert, update, delete.
trigger NoShareXLSX on ContentDocumentLink (after insert) { for (ContentDocumentLink cdl : trigger.new) { if (!CDLHelper.isSharingAllowed(cdl)) { cdl.addError('Sorry, you cannot share this file.'); } } }
public class CDLHelper { /** * Gets FileExtension of the inserted content. */ public static String getFileExtension(ContentDocumentLink cdl) { String fileExtension; String docId = cdl.ContentDocumentId; FileExtension = [select FileExtension from ContentVersion where ContentDocumentId = :docId].get(0).FileExtension; return FileExtension; } /** * Checks the file's PublishStatus and FileExtension to decide whether user can share the file with others. * PublishStatus 'P' means the document is in a public library. */ public static boolean isSharingAllowed(ContentDocumentLink cdl) { String docId = cdl.ContentDocumentId; ContentVersion version = [select PublishStatus,FileExtension from ContentVersion where ContentDocumentId = :docId].get(0); if (version.PublishStatus.equals('P') && (version.FileExtension != null && version.FileExtension.equals('xlsx'))) { return false; } return true; } /** * Gets the parent account name if the file is linked to an account. */ public static String getAccountName(ContentDocumentLink cdl) { String name; String id = cdl.LinkedEntityId; if (id.substring(0,3) == '001') { name = [select Name from Account where Id = :id].get(0).Name; } return name; } }
Apex has a per organization limit of 10 concurrent requests that last longer than 5 seconds. A trigger that uploads files can easily hit this limit.