Development questions
Write a trigger on ContentDocumentLink (File upload) to check if a file is uploaded to a Case and update Has_Attachment__c = true on the Case.
Files are linked to records through ContentDocumentLink (the LinkedEntityId is the Case Id). Use after insert and check the Case key prefix (500).
apex
trigger CDLTrigger on ContentDocumentLink (after insert) {
Set<Id> caseIds = new Set<Id>();
for (ContentDocumentLink l : Trigger.new) {
if (l.LinkedEntityId != null && l.LinkedEntityId.getSObjectType() == Case.SObjectType) caseIds.add(l.LinkedEntityId);
}
if (caseIds.isEmpty()) return;
List<Case> cases = new List<Case>();
for (Id id : caseIds) cases.add(new Case(Id = id, Has_Attachment__c = true));
update cases;
}
You can't write a trigger directly on ContentDocument for the upload link itself, since the link is created by ContentDocumentLink.
apex-triggertriggercodingscenariofiles
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?