You need to sign in to do that
Don't have an account?
Ricki Reay
APEX Trigger Help - Uncheck Custom Checkbox Field When File is Deleted
Hi there,
I am working on a custom APEX trigger that validates whether users have attached a file to a record in a custom object.
Custom Object: Advice
Context: users must upload a file to their Advice records in order to recieve credit for the record - this is a requirement for reporting. I have created a custom checkbox field called, "Has Attachment" (API: HasAttachment__c) on the Advice object and implemented a trigger that sets HasAttachment__c = true when a file is uploaded. However, I am now trying to add on to the trigger so that it sets HasAttachment__c = false if the file is subsequently deleted. That is, if the record becomes empty again (no file) after there previously was a file uploaded, then the checkbox would be = false.
Here is my trigger code so far...it is on the Content Document Link object:
I am working on a custom APEX trigger that validates whether users have attached a file to a record in a custom object.
Custom Object: Advice
Context: users must upload a file to their Advice records in order to recieve credit for the record - this is a requirement for reporting. I have created a custom checkbox field called, "Has Attachment" (API: HasAttachment__c) on the Advice object and implemented a trigger that sets HasAttachment__c = true when a file is uploaded. However, I am now trying to add on to the trigger so that it sets HasAttachment__c = false if the file is subsequently deleted. That is, if the record becomes empty again (no file) after there previously was a file uploaded, then the checkbox would be = false.
Here is my trigger code so far...it is on the Content Document Link object:
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) { List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new ); Set<ID> parentIds = New Set<ID>(); for ( ContentDocumentLink cdl : cdls ) { parentIds.add( cdl.LinkedEntityId ); } for ( List<Advice__c> adviceToUpdate: [ SELECT Id, ( SELECT Id FROM ContentDocumentLinks LIMIT 1 ) FROM Advice__c WHERE Id IN :parentIds ] ) { for ( Advice__c q : adviceToUpdate) { q.HasAttachment__c = true ; } update adviceToUpdate; } }If anyone could provide any help, suggestions or guidance, it would be GREATLY appreciated. Thanks in advance.
All Answers
Unfortunately, I tried the code you suggested above the the "Has Attachment" checkbox remains checked when I delete a file. Also, in the developer console is says there is a problem on line 6, saying "Loop must iterate over collection: Map". Please advise if able and thanks so much for your assistance on this!