function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chaitanya motupalli 13chaitanya motupalli 13 

Apex Trigger/Class for ContentDocument and ContentDocumentLink

trigger JJ_ContentDocument on ContentDocument (before delete) {
    
    if(trigger.isbefore &&trigger.isdelete){
        JJ_ContentDocumentTriggerHandler.beforeDeleteCountCRAttachments(Trigger.old);
    }
}


public class JJ_ContentDocumentTriggerHandler {
    
    public static void beforeDeleteCountCRAttachments(List<ContentDocument> ConDoclst){
        set<id> cdocid = new set<id>(); 
        set<id> clinkparids = new set<id>(); 
        
        for(ContentDocument cd : ConDoclst){
            cdocid.add(cd.id); 
        }
        list<ContentDocumentLink> clinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where ContentDocumentId IN :cdocid];
        for(ContentDocumentLink cl : clinks){
            clinkparids.add(cl.LinkedEntityId);
        }
        
        list<Coaching_Reports_Attachments_JNJ__c> coachinglist = [select id,JJ_No_Of_Content_Documents__c,No_of_Attachments_JNJ__c,(select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLinks) from Coaching_Reports_Attachments_JNJ__c where id IN:clinkparids];
        for(Coaching_Reports_Attachments_JNJ__c a :coachinglist){
            integer exsiz = a.ContentDocumentLinks.size();
            integer cdocsiz = cdocid.size();
            integer coutsiz = exsiz - cdocsiz;
            a.JJ_No_Of_Content_Documents__c= coutsiz;
        }
        if(coachinglist !=null && coachinglist.size()>0){
            try{
                update coachinglist;
            }catch(DmlException  ex) {
                system.debug('error message is'+ex.getMessage());
            }
            
        }
    }
    
}

trigger JJ_ContentDocumentLinkTrigger on ContentDocumentLink (after insert, before delete, after update, before update) {
    if(trigger.isInsert && trigger.isafter){
        JJ_ContentDocumentLinkTriggerHandler.updateJCPAttachments(Trigger.new);
        JJ_ContentDocumentLinkTriggerHandler.afterinsertcountCRAttachmentsAndNotes(Trigger.new);
    }
    
    if(trigger.isdelete && trigger.isbefore){
        JJ_ContentDocumentLinkTriggerHandler.beforedeletecountCRAttachmentsAndNotes(Trigger.old);
     }   
}


    public static void beforedeletecountCRAttachmentsAndNotes(list<ContentDocumentLink> ConDocLinklst){
     
        set<id> cdocid = new set<id>();
        set<id> deltinglinkid = new set<id>();
        for(ContentDocumentLink cl : ConDocLinklst){
            cdocid.add(cl.LinkedEntityId);
            deltinglinkid.add(cl.ContentDocumentId);
        }
        list<Coaching_Reports_Attachments_JNJ__c> coachinglist = [SELECT id,JJ_No_Of_Content_Documents__c,No_of_Attachments_JNJ__c,
                                                                  (SELECT Id, LinkedEntityId, ContentDocumentId 
                                                                   FROM ContentDocumentLinks) 
                                                                   FROM Coaching_Reports_Attachments_JNJ__c 
                                                                   WHERE id IN:cdocid];
        for(Coaching_Reports_Attachments_JNJ__c a :coachinglist){
            integer exsiz = a.ContentDocumentLinks.size();
            integer cdocsiz = deltinglinkid.size();
            integer coutsiz = exsiz -cdocsiz;
            a.JJ_No_Of_Content_Documents__c= coutsiz;
        }
        if(coachinglist !=null && coachinglist.size()>0){
            try{
               update coachinglist;
               system.debug('complete list'+coachinglist);
            }catch(DmlException ex) {
                system.debug('error message is'+ex.getMessage());
            }
         }
    }
Best Answer chosen by chaitanya motupalli 13
chaitanya motupalli 13chaitanya motupalli 13
Above post is answer itself. its not a question