public class Controller { // This method is called when a new ContentDocument is inserted public static void ControllerMethod(List cdlist) { try { String cdid = '', Title = ''; // Iterating over the inserted ContentDocuments for (ContentDocument cd : cdlist) { cdid = cd.Id; Title = cd.Title; } // Debugging the data system.debug('@@@@ getting data' + cdlist + 'ontent Document id:' + cdid); // Calling the checkDoc method with the Id and Title of the last ContentDocument in the list Controller.checkDoc(cdid, Title); } catch (Exception e) { system.debug('Error in ControllerMethod: ' + e); } } // This future method is used to perform operations that need to be done asynchronously @future public static void checkDoc(String docId, String Title) { try { // Fetching all ContentDocumentLink records associated with the given ContentDocument Id List cdlList = [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :docId]; system.debug('@@@@@ ContentDocumentLink Record:' + cdlList+'Title of content document:'+ Title); List LinkedEntyId = new List(); // Extracting the LinkedEntityIds from the fetched ContentDocumentLink records for (ContentDocumentlink cd1 : cdlList) { LinkedEntyId.add(cd1.LinkedEntityId); } system.debug('@@@@@ LinkedEntityId' + LinkedEntyId); // Fetching all Contact records associated with the LinkedEntityIds List cList = [SELECT Id, LastName, Profile_Image__c, Image_url__c, Description FROM Contact WHERE Id = :LinkedEntyId]; system.debug('Contact list where attachment is added' + cList); // Fetching the ContentVersion record associated with the given ContentDocument Id ContentVersion cv = [SELECT Id, ContentBodyId FROM ContentVersion WHERE ContentDocumentId = :docId]; system.debug('@@@@@ contentVersion records:' + cv); List updatedContacts = new List(); for (Contact contact : cList) { if (Title.containsIgnoreCase('Profile')) { // If the title of the document contains 'Profile', updating the Image_url__c and Description fields of the Contact record contact.Image_url__c = 'https://suprnova-dev-ed.develop.file.force.com/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Jpg&versionId=' + cv.Id + '&operationContext=CHATTER&contentId=' + cv.ContentBodyId; contact.Description = 'Profile Photo Updated'; } updatedContacts.add(contact); } // Updating the Contact records update updatedContacts; } catch (Exception e) { system.debug('Error in checkDoc: ' + e); } } static Id linkedEntityId; // This method is called when a ContentDocument is deleted public static void deletemethod(List cdlist){ String oldId=''; // Iterating over the deleted ContentDocuments for(ContentDocument p:cdlist){ oldId=p.Id; } system.debug('@@@@@ deleted document id:'+oldId); ContentDocumentLink cdl = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :oldId LIMIT 1]; system.debug('@@@@@cdl:'+cdl); linkedEntityId = cdl.LinkedEntityId; system.debug('@@@@@linkedEntityId:'+linkedEntityId); // Calling the checkDoc2 method with the Id of the last deleted ContentDocument in the list Controller.checkDoc2(linkedEntityId); } // This future method is used to perform operations that need to be done asynchronously after a ContentDocument is deleted @future public static void checkDoc2(Id linkedEntityId){ try{ system.debug('Entered In delete Method'+linkedEntityId); // Fetching the LinkedEntityId from the ContentDocumentLink record associated with the given ContentDocument Id // ContentDocumentLink odlEntityId=[SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId=:docId2]; // system.debug('@@@@@ linked entity id:'+odlEntityId); // Fetching all ContentDocuments related to the same Contact as the deleted document ContentDocumentLink allRelatedCd=[SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:linkedEntityId]; system.debug('###allRelatedCd'+allRelatedCd); List idTitles=[SELECT Id,Title FROM ContentDocument WHERE Id=:allRelatedCd.ContentDocumentId ]; ListidofProfile=new List(); for(ContentDocument vb:idTitles){ if(vb.Title.containsIgnoreCase('Profile')){ idofProfile.add(vb.Id); } } system.debug('@@@@@ idofProfile'+idofProfile); // Fetching the latest ContentDocument with a title containing 'Profile' ContentDocument contentList0=[SELECT Id,Title FROM ContentDocument WHERE Id =:idofProfile ORDER BY CreatedDate DESC LIMIT 1]; system.debug('@@@@@ ids of profile named files:'+ contentList0); List updatedContacts = new List(); // Fetching all Contact records associated with the LinkedEntityId ListcList=[SELECT Id,LastName,Profile_Image__c,Image_url__c,Description FROM Contact WHERE Id =:linkedEntityId]; system.debug('Contact list where attachment is added'+cList); // Storing the Id and title of the fetched ContentDocument if(contentList0!=null){ String profileid=contentList0.Id; String Title=contentList0.Title; system.debug('@@@@@ List of ids:'+profileid); system.debug('@@@@@ List of ids:'+Title); // Fetching the LinkedEntityId from the ContentDocumentLink record associated with the fetched ContentDocument //ListcdLinkList=[SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId=:profileid]; // String entityId=''; //for(ContentDocumentLink cdo:cdLinkList){ // entityId=cdo.LinkedEntityId; //} //system.debug('@@@@ linked entity id :'+ entityId); // Fetching the ContentVersion record associated with the fetched ContentDocument ContentVersion cv = [SELECT Id,ContentBodyId FROM ContentVersion WHERE ContentDocumentId = :profileid]; system.debug('@@@@@ contentVersion records:'+cv); // Updating the Contact records for (Contact contact : cList) { contact.Image_url__c='https://suprnova-dev-ed.develop.file.force.com/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Jpg&versionId='+cv.Id+'&operationContext=CHATTER&contentId='+cv.ContentBodyId; contact.Description = 'previous profile photo updated'; updatedContacts.add(contact); } } else{ for (Contact contact : cList) { contact.Image_url__c='No profile'; contact.Description = 'No previous profile '; updatedContacts.add(contact); } } update updatedContacts; } catch(Exception e){ System.debug('@@@@@@ error:'+e); } } }