5

I know that ContentDocumentLink triggers are not bulkified by Salesforce. I was expecting that behavior and was writing test classes accordingly.

It looks like, ContentDocumentLinkTrigger doesn't respect the custom settings values inserted in the test context and uses the default value for that field.

Minimum Viable Code to Reproduce:

Hierarchical Custom Setting:

Name : OrgSettings__c

Field : Enable_CDL_Count__c type=checkbox default= false

Trigger:

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
    switch on Trigger.operationType {
       when  AFTER_INSERT{
           ContentDocumentLinkTriggerHandler.handleAfterInsert(Trigger.new); 
       }

    }

Trigger Handler:

public  class ContentDocumentLinkTriggerHandler {

    @TestVisible
    private static Integer count =0;

    public static void handleAfterInsert(List<ContentDocumentLink> linkList) {
        //
        System.debug('Settings Value'+OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c);
        if(OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c){ 
            count ++;  
        }


    }

  }
}

And Finally Test Class:

@isTest
public class ContentDocumentLinkTriggerHandlerTest {

    @istest
    static void testBulkFileUploadWithCustomSetting(){

        //Setup Dat

      Account acc = new Account(Name='Dummy Account');
      insert acc;

      //creating 200 content version
      List<ContentVersion> cvListToBeInserted = new List<ContentVersion>();

      for(Integer i=0; i<200; i++){
          ContentVersion contentVersionInsert = new ContentVersion(
                Title = 'Test',
                PathOnClient = 'Test.jpg',
                VersionData = Blob.valueOf('Test Content Data'),
                IsMajorVersion = true
            );
        cvListToBeInserted.add(contentVersionInsert);

        }

        insert cvListToBeInserted;

        List<ContentVersion> insertedContentVersionList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id IN : cvListToBeInserted ]; 

        //Creating corresponding contentDocumentLink
        List<ContentDocumentLink> cvlinkListToBeInserted = new List<ContentDocumentLink>();
        for(ContentVersion contentVersionSelect : insertedContentVersionList){

            ContentDocumentLink contentlink = new ContentDocumentLink();
            contentlink.ShareType = 'V';
            contentlink.LinkedEntityId = acc.Id;

            contentlink.ContentDocumentId = contentVersionSelect.ContentDocumentId;
            contentlink.Visibility = 'AllUsers';            
            cvlinkListToBeInserted.add(contentlink);
        }

        System.assertEquals(0, ContentDocumentLinkTriggerHandler.count);
        OrgSettings__c orgs = OrgSettings__c.getOrgDefaults();
        orgs.Enable_CDL_Count__c = true ; 
        upsert orgs;

        test.startTest();
        insert cvlinkListToBeInserted;
        test.stopTest();  

        System.assertEquals(200, ContentDocumentLinkTriggerHandler.count); 
        //Assert Fails System.AssertException: Assertion Failed: Expected: 200, Actual: 1

    }
}

As you can see,I am upserting the Org default for custom settings as true and I am incrementing count variable to count how many time handler was called. Also, I added a switch to test that behavior using same Hierarchical custom settings(OrgSettings__c).

When I ran the test class, it fails at assert. So apparently count value is 1 , which is just weird. On adding system.debug in my handler I found

OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c is false 199 times and is true 1(last record) time and hence the assert failed.

This is super weird, and I don't have any explanation for it. Can someone shed some light why this is happening?

2

I think the document that you have linked in the beginning of the question (bulkified) is incorrect. From what I just observed, the execution of the triggers is the other way around. That is, when you insert ContentVersion records, the triggers on ContentDocumentLink are executed in a non-bulkified manner. But when you insert a list of records in ContentDocumentLink, the trigger executes in a bulkified manner.

I carried out the following 2 tests:

Test 1:
Add a System.debug just before and after the line where you insert ContentVersion records.

system.debug('ContentVersion insert BEGIN');
    insert cvListToBeInserted;
system.debug('ContentVersion Insert END');

And also add a system.debug before and after the line where you insert the ContentDocumentLink records.

system.debug('CVLink insert BEGIN');

    test.startTest();
    insert cvlinkListToBeInserted;
    test.stopTest();  

    system.debug('CVLink insert END');

Now run the test class. What you will observe is, the Settings Value is displayed as False 200 times before the 'CVLink insert BEGIN' statement is displayed in the log.
Then, it is only displayed on time after the 'CVLink insert BEGIN' has been printed. Which means the trigger handler on the ContentDocumentLink was being called when the ContentVersion was being inserted as well.

Test 2:
Upsert the custom setting right in the beginning of your test class with Enable_CDL_Count__c as True.

Now run the test class, the test class will fail at the first assert statement itself.
Assertion Failed. Expected: 0, Actual: 200

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.