Trying to create a trigger that will update a field if an attachment related to it is added or deleted.
Here's what I have so far
trigger updateAttachments on ContentDocumentLink (after insert, after delete) {
List<Opportunity> Opps = new List<Opportunity>();
if(trigger.isInsert){
for(ContentDocumentLink c: Trigger.New){
Opps = [SELECT id, hasAttachment__C from Opportunity WHERE id =: c.LinkedEntityId];
}
for(Opportunity o: Opps){
o.hasAttachment__c = True;
}
}
if(trigger.isDelete){
for(ContentDocumentLink d:Trigger.Old){
Opps = [SELECT id, hasAttachment__C from Opportunity WHERE id =: d.LinkedEntityId];
}
for(Opportunity op: Opps){
op.hasAttachment__c = False;
}
}
update Opps;
}
The trigger is working when adding a new record, the problem is when I delete an attachment the checkbox does not update.
Please give me an insight on why it's not getting triggered when I delete an attachment
Thank you!