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

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

Sample Code:

Set < Id > setContentVerIds = new Set < Id >();
List < ContentDocument > listFiles = new List < ContentDocument >();

for ( FeedComment objFC : [ SELECT Id, RelatedRecordId FROM FeedComment ] ) { 
    
    //Add your required where condition
    if ( String.isNotBlank( objFC.RelatedRecordId ) ) {
        
        //RelatedRecordId is the Content Version Id
        setContentVerIds.add( objFC.RelatedRecordId );
        
    }
    
}

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;
    
}

Leave a Reply