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
SambitNayakSambitNayak 

Test Class for ContentDocumentLink (Notes)

Hi,

I have a requirement where a field of the parent object changes everytime the user enters a NOTE.
I wrote a trigger on ContentDocumentLink (everytime a ContentDocumentLink record is created, the parent record's field changes) and it's working fine. However my Test Class is not allowing me to create 200 ContentDocumentLink records. Please review the code below and help if you have ever experienced such issues.
It's exceeding the governor limits:

Please note: CA.Id is my parent object (I have already inserted it in my earlier part of the code)

 

 

        for(Integer a=0;a<200;a++){
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');
        ContentVersion cVersion = new ContentVersion(ContentLocation = 'S', PathOnClient = 'testPath', Title = 'testTitle’+a,
                                                    VersionData = beforeblob);
        contVerToInsert.add(cVersion);
        }
Insert contVerToInsert;
        
        for (ContentVersion cv:contVerToInsert){
    mapIndexToContVer.put(Integer.valueOf(cv.Title.remove('testTitle')),cv.Id);
}

        
        for(Integer j=0;j<200;j++){
                   ContentDocumentLink CDL = New ContentDocumentLink(LinkedEntityId=CA.Id, ShareType = 'V',
                                                              ContentDocumentId=mapIndexToContVer.get(j));

            notesToInsert.add(CDL);
        }

        Test.startTest();
        Insert notesToInsert;
        Test.stopTest();         
        
}



 

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you let us know what governer limit it is exceeding?

I tried this piece of code in my org and it got executed with out any issue which means the code is correct except that your logic is assiging content version id to content document in the below line.
 
mapIndexToContVer.put(Integer.valueOf(cv.Title.remove('testTitle')),cv.Id);
 
a                   ContentDocumentLink CDL = New ContentDocumentLink(LinkedEntityId=acc.id, ShareType = 'V',ContentDocumentId=mapIndexToContVer.get(j));


I have tried the below code and it worked with out any issue.
 
Account acc=[select id from Account where id ='0015j00000nEX8gAAG']; 
List<ContentVersion>contVerToInsert= new List<ContentVersion>();
List<ContentDocumentLink>notesToInsert= new List<ContentDocumentLink>();
Map<Integer,id> mapIndexToContVer= new map<integer,id>();
for(Integer a=0;a<200;a++){
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');
        ContentVersion cVersion = new ContentVersion(ContentLocation = 'S', PathOnClient = 'testPath', Title = 'testTitle'+a,
                                                    VersionData = beforeblob);
        contVerToInsert.add(cVersion);
        }
Insert contVerToInsert;

List<ContentVersion> cvlist= [select id,contentdocumentid from ContentVersion where PathOnClient='testPath'];
        
        

        
        for(ContentVersion cvs:cvlist){
                   ContentDocumentLink CDL = New ContentDocumentLink(LinkedEntityId=acc.id, ShareType = 'V',ContentDocumentId=cvs.contentdocumentid);

            notesToInsert.add(CDL);
        }


        Insert notesToInsert;



I guess your customization is causing some issue there.


If this solution helps, Please mark it as best answer.

Thanks,
SambitNayakSambitNayak

Hi Sai, Thanks so much for your help. But, my code is not even running for 90 records... It still says "Too many SOQL queries: 101".

Any idea where this error could be coming from ??

Sai PraveenSai Praveen (Salesforce Developers) 
hi Sambit,

The issue may be because of some trigger on Content document or content document link  which is not ready for bulk records.

Thanks,