Last active
February 27, 2017 06:31
-
-
Save douglascayers/38a75d41ecd4f285ac96745a40d6a42b to your computer and use it in GitHub Desktop.
Example triggers for detecting when links to Chatter Files are deleted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger AttachmentTrigger on Attachment ( before delete ) { | |
Set<ID> parentIds = new Set<ID>(); | |
for ( Attachment a : Trigger.old ) { | |
parentIds.add( a.parentId ); | |
} | |
System.debug( parentIds ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( before delete ) { | |
Set<ID> entityIds = new Set<ID>(); | |
for ( ContentDocumentLink cdl : Trigger.old ) { | |
entityIds.add( cdl.linkedEntityId ); | |
} | |
System.debug( entityIds ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger ContentDocumentTrigger on ContentDocument ( before delete ) { | |
Set<ID> contentDocumentIds = new Set<ID>(); | |
for ( ContentDocument cd : Trigger.old ) { | |
contentDocumentIds.add( cd.id ); | |
} | |
List<ContentDocumentLink> cdls = new List<ContentDocumentLink>([ | |
SELECT | |
id, | |
contentDocumentId, // document being shared | |
linkedEntityId, // who/what the file is shared to | |
shareType // view, edit, owner access to file | |
FROM | |
ContentDocumentLink | |
WHERE | |
contentDocumentId IN :contentDocumentIds | |
]); | |
System.debug( cdls ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Doug, thanks for this code. I finally got my triggers to pick up when feed item-level files and regular "Notes & Attachment" notes are added or deleted.
Now I'm struggling with the test class. I'm trying to add content documents, content document links, and I think I'm lost.
Below is what I currently have. On the line for feedItem.ContentFileName I'm either getting an error for 'ContentFileName' not being a valid attribute, or, when I comment out that line, I get an error for the ContentFileName being required. I'm confused... I hope you can help.
` //Insert contentdocument data
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
//insert new ContentDocumentLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent2.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';`