Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active February 27, 2017 06:31
Show Gist options
  • Save douglascayers/38a75d41ecd4f285ac96745a40d6a42b to your computer and use it in GitHub Desktop.
Save douglascayers/38a75d41ecd4f285ac96745a40d6a42b to your computer and use it in GitHub Desktop.
Example triggers for detecting when links to Chatter Files are deleted.
trigger AttachmentTrigger on Attachment ( before delete ) {
Set<ID> parentIds = new Set<ID>();
for ( Attachment a : Trigger.old ) {
parentIds.add( a.parentId );
}
System.debug( parentIds );
}
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( before delete ) {
Set<ID> entityIds = new Set<ID>();
for ( ContentDocumentLink cdl : Trigger.old ) {
entityIds.add( cdl.linkedEntityId );
}
System.debug( entityIds );
}
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 );
}
@realdavidberman
Copy link

realdavidberman commented Feb 27, 2017

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;

     ContentVersion testContent2 = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];

     FeedItem feeditem = new FeedItem();
    feedItem.Type = 'ContentPost';
    feedItem.Body = 'Place holder file. Upload attachmenet using Upload new version.';
    feedItem.ContentData = beforeblob;
    feedItem.ContentFileName = 'Test File';
    feedItem.ParentId = exp.id;
    insert feedItem;

//insert new ContentDocumentLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent2.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment