How to delete files attached to FeedComment after deleting FeedIComment in Salesforce?

Sample Code:

Set < Id > setContentVerIds = new Set < Id >();
for ( FeedComment FC : [ SELECT Id, RelatedRecordId FROM FeedComment ] ) { 
        //Add your required where condition
if ( String.isNotBlank(FC.RelatedRecordId) )
setContentVerIds.add(FC.RelatedRecordId);//RelatedRecordId is the Content Version Id
}
List < ContentDocument > listFiles = new List < ContentDocument >();
for ( ContentVersion CV : [ SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id IN: setContentVerIds ] )
listFiles.add(new ContentDocument(Id = CV.ContentDocumentId));
if ( listFiles.size() > 0 )
delete listFiles;

Cheers!!!

No comments:

Post a Comment