I'm getting the "Too many SOQL queries: 101" when executing a test class. I'm not even getting into my test method - it's failing on the test setup. When it fails, it's failing in my trigger helper class. Specifically, the error message is:
Class.ContentDocumentLinkTriggerHelper.myTriggerMethod: line 18, column 1 Trigger.ContentDocumentLinkTrigger: line 3, column 1
I know code needs bulkification and not do SOQL inside for loops, and as far as I can see, I have bulkified, and I'm not doing SOQL in a loop. My trigger is before insert on ContentDocumentLink, and in my test setup method, I'm not even inserting into ContentDocumentLink - I'm only inserting into ContentVersion. I'm using a loop to create 201 ContentVersion records, adding each one to a list, and outside the loop inserting. (Trying to simulate what might happen if someone imports multiple records simultaneously.)
I think what's happening is when I insert the ContentVersion, Salesforce is automatically creating a ContentDocumentLink record (not unexpected), which fires my trigger. In my trigger helper class, I do have a SOQL statement outside the for loop, and I think it's bulkified, but the automatic ContentDocumentLink inserts Salesforce is doing are happening one at a time and not bulkified, causing my SOQL statement to execute multiple times, causing the error, but I can't figure out why.
Can anyone shed some light on what is happening? Is my test of trying to insert 201 ContentVersion records invalid? The code below is from a dev org, and you should be able to copy/paste into a dev org if you want and reproduce the issue. Just run the test method, and you should get the failure message.
My trigger:
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
if (trigger.isBefore && trigger.isInsert) {
ContentDocumentLinkTriggerHelper.myTriggerMethod(trigger.new);
}
}
Trigger helper:
public with sharing class ContentDocumentLinkTriggerHelper {
public static void myTriggerMethod(List<ContentDocumentLink> insertedContentDocLinks) {
system.debug(insertedContentDocLinks);
List<ContentDocumentLink> filteredContentDocLinks = new List<ContentDocumentLink>();
List<String> contentDocumentIds = new List<String>();
for (ContentDocumentLink contDocLink : insertedContentDocLinks) {
string entityId = contDocLink.LinkedEntityId;
if (entityId.left(3) == '005') {
filteredContentDocLinks.add(contDocLink);
contentDocumentIds.add(contDocLink.ContentDocumentId);
}
}
system.debug('contentDocumentIds: ' + contentDocumentIds);
// The below line is where the error is reported.
List<ContentDocument> contentDocs = [select id, title, FileType from ContentDocument where id in :contentDocumentIds];
//Map<Id, ContentDocument> contentDocumentMap = new Map<Id, ContentDocument>([select id, title, FileType from ContentDocument where id in :contentDocumentIds]);
// Do other work here with filteredContentDocLinks
}
}
Test class:
@isTest
public with sharing class ContentDocumentLinkTriggerHelperTest {
@testSetup
static void setupData(){
Account a = new Account(
Name = 'Test Account 1'
);
insert a;
Opportunity o = new Opportunity(
Name = 'Test Opportunity 1',
AccountId = a.Id,
CloseDate = Date.newInstance(2022, 07, 22),
StageName = 'Prospecting'
);
insert o;
List<ContentVersion> contentVersions = new List<ContentVersion>();
for (Integer i = 1; i <= 201; i++) {
String fileExt = '.pdf';
if (i <= 100) {
fileExt = '.docx';
}
ContentVersion contentVersion = new ContentVersion(
Title = 'Invoice 2022-07-09 - ' + String.valueOf(i) + fileExt,
PathOnClient = 'Invoice 2022-07-09 - ' + String.valueOf(i) + fileExt,
VersionData = Blob.valueOf('This is my sample content - ' + String.valueOf(i)),
IsMajorVersion = true
);
contentVersions.add(contentVersion);
}
system.debug('inserting contentVersions');
insert contentVersions;
}
@isTest
static void myTestMethod() {
system.assertEquals(1, 1, 'Fake Message');
}
}