With this ContentDocumentLink
trigger handler:
@TestVisible private static Integer callCount = 0;
// Entry point from trigger
public static void handle(
TriggerOperation op,
Map<Id, ContentDocumentLink> oldMap,
Map<Id, ContentDocumentLink> newMap) {
callCount++;
...
}
this test passes:
private static void insertFile(Id parentId) {
ContentVersion file = new ContentVersion(
PathOnClient = 'File - ' + parentId + '.txt',
FirstPublishLocationId = parentId,
Title = 'File Title ' + parentId,
VersionData = Blob.valueOf('File Body ' + parentId)
);
insert file;
}
@IsTest
static void checkTriggerIsCalled() {
Account a = new Account(Name = 'Acme');
insert a;
System.assertEquals(0, ContentDocumentLinkHandler.callCount);
insertFile(a.Id);
System.assertEquals(1, ContentDocumentLinkHandler.callCount);
}
so this creation pattern is NOT the cause of my problem.
The only other reason I can think of for my trigger not running is e.g. a governor limit being hit in some other ContentDocumentLink
related trigger before my trigger is reached. Any other ideas?
PS
I did not succeed in finding the cause of this so have implemented a work-around where a ContentVersion
trigger is also used to track the insert
with this class used in the logic:
private class Link {
Id entityId;
Id contentId;
Integer hashCode;
/** Supports ContentDocumentLink and ContentVersion. */
Link(SObject sob) {
if (sob.getSObjectType() == ContentDocumentLink.SObjectType) {
ContentDocumentLink cdl = (ContentDocumentLink) sob;
entityId = cdl.LinkedEntityId;
contentId = cdl.ContentDocumentId;
} else if (sob.getSObjectType() == ContentVersion.SObjectType) {
ContentVersion cv = (ContentVersion) sob;
entityId = cv.FirstPublishLocationId;
contentId = cv.ContentDocumentId;
}
}
public Boolean equals(Object o) {
Link that = (Link) o;
return this.entityId == that.entityId
&& this.contentId == that.contentId;
}
public Integer hashCode() {
if (hashCode == null) {
hashCode = 0;
if (entityId != null) hashCode += System.hashCode(entityId);
if (contentId != null) hashCode += System.hashCode(contentId) * 31;
}
return hashCode;
}
}
with duplicates blocked via a set of these objects.
PPS
See System.hashCode still broken for Id for the cause of a bug in the above class and how to fix it.