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
Genious007Genious007 

bulkify the ContentDocument Trigger

I need some help to bukify the ContentDocument trigger. I am hitting the limit on ContentVersion CV_FileName = [select id, title, FileExtension from ContentVersion where ContentDocumentId = :mapOrpIdWithContentId.Values()];


*******Trigger*****
trigger trgPhotoUploader on ContentDocumentLink (after insert) {

    
   // Orp picture Pattern to search for
    Pattern PatternOrpPicture = pattern.compile('[a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9].[jJ][pP][gG]');
    // APR Activity photo 1 pattern 
    Pattern PatternActPhoto1 = pattern.compile('[a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9][aA].[jJ][pP][gG]');
    // APR Activity photo 2 pattern 
    Pattern PatternActPhoto2 = pattern.compile('[a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9][bB].[jJ][pP][gG]');

 
    Map<Id, String> mapOrpIdWithContentId = new Map<Id, String>();
 if(trigger.isinsert) {  
     for (ContentDocumentLink CDLink : trigger.new) {
         mapOrpIdWithContentId.put(CDLink.LinkedEntityId,CDLink.ContentDocumentId); 
        system.debug('values Doc='+CDLink.ContentDocumentId+'='+CDLink.LinkedEntityId+'='+ CDLink.Id);
     }// for

     ContentVersion CV_FileName = [select id, title, FileExtension from ContentVersion where ContentDocumentId = :mapOrpIdWithContentId.Values()];
    
   // Get list of Orps records to update
    List<Orphan__c> OrphansToUpdate = new List<Orphan__c>();
    for (Orphan__c o : [SELECT Id, Orphan_Id_Number__c, Orphan_Image__c
                        FROM Orphan__c WHERE Id IN :mapOrpIdWithContentId.keySet()])
    {
        if(mapOrpIdWithContentId.get(o.Id)!= null){
      Matcher MatcherOrpPicture = PatternOrpPicture.matcher(CV_FileName.title+'.'+CV_FileName.FileExtension);   
              if (MatcherOrpPicture.matches())
                 {
                o.Orphan_Image__c = mapOrpIdWithContentId.get(o.Id);
                  OrphansToUpdate.add(o);
               }
        }
    }// for
    if(OrphansToUpdate.size()>0){ update OrphansToUpdate;}