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
MilesSMilesS 

Bulkify Content Documents

Hey,

I am trying to bulkify some code on Salesforce for the new "Content" objects.  I want to either count or add a link to the opportunity when a new document/file is inserted or deleted if the phrase “RFAssessment” is contained in the Title.  I have successfully built a trigger & class to take care of this, but the SOQL & DML statements are currently within my for loop.  My knowledge of code is a 101 class to C++, python & GTSOOI (Google the sh!t out of it).  If I should be looking at a different trigger object or if someone could show me how every content object is related (because ContentDocumentLink & ContentDocument seem to be left out of most descriptions) I will be extremely grateful!


The following bit of code produces an error...
trigger ContentDocLink_TEST on ContentDocumentLink (before insert) {
    List<ContentDocumentLink> CDL = [SELECT Id,LinkedEntityId,ContentDocumentId,
                                     	(SELECT Id,Title FROM ContentDocument)
                                     FROM ContentDocumentLink 
                                     WHERE Id IN :Trigger.newMap.keySet()];
}



!!!!!ERROR MESSAGE!!!!!
(SELECT Id,Title FROM ContentDocument)
                              ^ERROR at Row:2:Column:61
Didn't understand relationship 'ContentDocument' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."


The following code works, but at a cost of 5 SOQL quieres per file.  Not good for bulk uploading.

 
MilesSMilesS
trigger ContentDocLink_Trigger on ContentDocumentLink (after insert, after update, before delete) {
    system.debug('-------------------Start Trigger: ContentDocLink_Trigger on ContentDocumentLink-------------------');    
    if(trigger.isafter){
        for(ContentDocumentLink CDL:trigger.new) {
            String NoteParent = CDL.LinkedEntityId;
            String ContentID = CDL.ContentDocumentId;
            String TriggerType = NULL;
            if(trigger.isInsert || trigger.isUnDelete){
                TriggerType = 'Insert';
            }else if(trigger.isdelete){
                TriggerType = 'Delete';
            }
            ContentDocument CD = [SELECT Title FROM ContentDocument WHERE Id=:ContentID];
            String Title = String.valueOf(CD.Title);
            System.debug('Title: '+Title);
            system.debug('Id: '+ContentID);
            if(NoteParent.left(3)=='006'){
                system.debug('.............Start Class: OpportunityRollUp.NoteCount.............');	
                system.debug('Criteria for entry: Parent ID must start w/ "006" or must be from the opportunity table');
                OpportunityRollUp.NoteCount(NoteParent,Title,ContentID,TriggerType);
                system.debug('.............End Class: OpportunityRollUp.NoteCount.............');
            }
        }
    }else if(trigger.isbefore){
        for(ContentDocumentLink CDL:trigger.old){
            String NoteParent = CDL.LinkedEntityId;
            String ContentID = CDL.ContentDocumentId;
            String TriggerType = NULL;
            if(trigger.isInsert || trigger.isUnDelete){
                TriggerType = 'Insert';
            }else if(trigger.isdelete){
                TriggerType = 'Delete';
            }
            ContentDocument CD = [SELECT Title FROM ContentDocument WHERE Id=:ContentID];
            String Title = String.valueOf(CD.Title);
            System.debug('Title: '+Title);
            system.debug('Id: '+ContentID);
            if(NoteParent.left(3)=='006'){
                system.debug('.............Start Class: OpportunityRollUp.NoteCount.............');	
                system.debug('Criteria for entry: Parent ID must start w/ "006" or must be from the opportunity table');
                OpportunityRollUp.NoteCount(NoteParent,Title,ContentID,TriggerType);
                system.debug('.............End Class: OpportunityRollUp.NoteCount.............');
            }
        }
    }
    system.debug('-------------------End Trigger: ContentDocLink_Trigger on ContentDocumentLink-------------------');
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi,

I have through your question. The error in code is because you are using the wrong inner query. The inner query will only work when you will access the child with a parent.
But in your code, you are trying to access the parent with child.
To correct your code please use the query given below:--

List<ContentDocument> CDL = [SELECT Id,Title,(SELECT Id,LinkedEntityId,ContentDocumentId FROM ContentDocumentLinks)
                                     FROM ContentDocument];
System.debug('ContentDocument--->'+CDL); 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
MilesSMilesS
When I copied this in, it appeared to work until I try to integrate into a for loop.  I recieved an error because "ContentDocumentLinks" does not exist (Invalid type: ContentDocumentLinks).  When I remove the "s" I get more errors.  Any other thoughts on this?

LinkedEntityId,ContentDocumentId FROM ContentDocumentLink) 
                                      ^
ERROR at Row:2:Column:88
Didn't understand relationship 'ContentDocumentLink' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.