0

I'm currently testing a trigger on ContentDocumentLinks.

In order to check in a test class if it bulkifies correctly, I first try to create many contentVersions (so they can the be added to ContentDocumentLink) using the following code, which stops working with a too many SOQL requests if I attempt to create 47 or more ContentVersions.

    List<ContentVersion> lcv = new List<ContentVersion>();
    for(Integer i=0;i<MAX;i++) {
        ContentVersion cv = new ContentVersion();
        cv.ContentLocation = 'S';
        cv.VersionData = res.body;
        cv.Title = 'TestPDF' + i;
        cv.PathOnClient = 'TestPDF' + i + '.pdf';
        lcv.add(cv);            
    }  
    System.debug('DML Statements before version creation : ' + Limits.getDmlStatements());
    System.debug('DML Rows before version creation : ' + Limits.getDmlRows());
    System.debug('SOQL Queries before version creation : ' + Limits.getQueries());        
    insert lcv;
    System.debug('DML Statements after version creation : ' + Limits.getDmlStatements());
    System.debug('DML Rows after version creation : ' + Limits.getDmlRows());
    System.debug('SOQL Queries after version creation : ' + Limits.getQueries());  

If MAX is set to 46, as a result, I get 5 queries before content version insertion (from previous test setup), 97 after ! This would imply that each contentVersion insertion causes 2 SOQL requests to execute (MAX being set to 46)

enter image description here

Can anyone confirm ?

  • To be sure, you have no triggers on ContentVersion or ContentDocumentLink or such? – Charles T Aug 8 at 12:28
  • Indeed had one on ContentDocumentLink, hence the problem (see below) – Frederic FINET Aug 8 at 15:35
1

Finally found out and I thought you may want to know if you run in to a similar problem : Creating a ContentVersion automatically creates a ContentDocumentLink on the running user. Unfortunately, this creation is not handled in a bulk. As a result, if you have a trigger on ContentDocumentLink, it will be call as many times as ContentVersions being added. You may then want to be extra careful if you have such a trigger when then entity is a User and not run ant SOQL in that case...

  • Very interesting. Are the CDL records already created by the time the after-trigger runs on ContentVersion? If so, you may be able to handle the bulk behaviour by having the CV trigger query the CDLs in bulk and do the processing in bulk. Might require some work to ensure CDL trigger doesn't fire when the insert is part of a CV insert trigger context, but does fire if CDL alone was inserted. – Charles T Aug 8 at 20:53
  • actually the cdl are firing the after trigger. Right now, problem solved as the after trigger now ilters valid entityIds (in my case, Ids for one custom object) before running any SOQL. I've considered however moving the trigger to cv as you're suggesting, but that would only work for freshly created cvs. So there might be a problem if you want a trigger to run when you share an existing cv to a new user, which, to my understanding will create a new cdl but not a new cv, and I see no way to distinguish that creation from on cdl created when cv is being created. (fortunately, not my use case) – Frederic FINET Aug 9 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.