I am trying to do (what I think is) a simple trigger that counts the number of files attached to a custom object record(SBQQ__QuoteDocument__c) and puts that number in a field on that object. I am trying to query ContentDocumentLink because after some research that seems like the correct object that holds the Quote Document Id(as LinkedEntityId) and the File Id in the same place. I am doing this so I can run validation rules to make sure a user has uploaded a file to each Quote Document record before Opportunity can be marked Closed Won, that part will be easy. I do not want to go the DLRS route or install any new apps because client is wary of doing this, so trigger seems like the best way to go. I don't get into into backend dev work very much so I get rusty easily. Here is what I have so far, neither will upload to the database successfully. Thank you in advance for any kind words of wisdom!
ContentDocumentLinkTrigger
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert, after delete, after undelete) {
if(trigger.isAfter){
if(trigger.isInsert || trigger.isDelete || trigger.isUndelete){
ContentDocumentLinkTriggerHandler.fileCount(Trigger.new);
}
}
}
ContentDocumentLinkTriggerHandler.cls
public with sharing class ContentDocumentLinkTriggerHandler {
public static void fileCount(Trigger.new){
Set<Id> parentQuoteDocumentIds = new Set<Id>();
for(ContentDocumentLink cdl : Trigger.new){
parentQuoteDocumentIds.add(cdl.LinkedEntityId);
}
List<SBQQ__QuoteDocument__c> quoteDocList = [SELECT Id FROM SBQQ__QuoteDocument__c WHERE Id IN :parentQuoteDocumentIds];
for(SBQQ__QuoteDocument__c qd : quoteDocList){
qd.Number_of_Files_Attached__c = quoteDocList.size();
}
}
}