function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ricki ReayRicki 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: 
 
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.
Best Answer chosen by Ricki Reay
Tad Aalgaard 3Tad Aalgaard 3
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) {

    Map<Id,ContentDocumentLink> cdls = ( Trigger.newMap == null ? Trigger.oldMap : Trigger.newMap );
    Set<ID> parentIds = New Set<ID>();

   for ( Id key : cdls.keySet() ) {
        parentIds.add( cdls.get(key).LinkedEntityId );
    }

    List<Advice__c> advicesToUpdate = new List<Advice__c>();

    for ( List<Advice__c> advices: [ SELECT Id, ( SELECT Id FROM ContentDocumentLinks LIMIT 1 ) FROM Advice__c WHERE Id IN :parentIds] ) {
        for ( Advice__c a : advices) {
            if(a.ContentDocumentLinks.size() > 0){
                a.HasAttachment__c = true ;
            }else{
                a.HasAttachment__c = false ;                             
            }
            advicesToUpdate.add(a);   
        }
    }

    update advicesToUpdate;  // do DML outside of SOQL to prevent limits
}

 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
I haven't run this code, but below are suggestions for you.
 
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) {

    Map<Id,ContentDocumentLink> cdls = ( Trigger.newMap == null ? Trigger.oldMap : Trigger.newMap );
    Set<ID> parentIds = New Set<ID>();

    for ( ContentDocumentLink cdl : cdls ) {
        parentIds.add( cdl.LinkedEntityId );
    }

    List<Advice__c> advicesToUpdate = new List<Advice__c>();

    for ( List<Advice__c> advices: [ SELECT Id, ( SELECT Id FROM ContentDocumentLinks LIMIT 1 ) FROM Advice__c WHERE Id IN :parentIds] ) {
        for ( Advice__c a : advices) {
            if(a.ContentDocumentLinks.size() > 0){
                a.HasAttachment__c = true ;
            }else{
                a.HasAttachment__c = false ;                             
            }
            advicesToUpdate.add(a);   
        }
    }

    update advicesToUpdate;  // do DML outside of SOQL to prevent limits
}
Ricki ReayRicki Reay
@Tad Aalgaard 3

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!
Tad Aalgaard 3Tad Aalgaard 3
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) {

    Map<Id,ContentDocumentLink> cdls = ( Trigger.newMap == null ? Trigger.oldMap : Trigger.newMap );
    Set<ID> parentIds = New Set<ID>();

   for ( Id key : cdls.keySet() ) {
        parentIds.add( cdls.get(key).LinkedEntityId );
    }

    List<Advice__c> advicesToUpdate = new List<Advice__c>();

    for ( List<Advice__c> advices: [ SELECT Id, ( SELECT Id FROM ContentDocumentLinks LIMIT 1 ) FROM Advice__c WHERE Id IN :parentIds] ) {
        for ( Advice__c a : advices) {
            if(a.ContentDocumentLinks.size() > 0){
                a.HasAttachment__c = true ;
            }else{
                a.HasAttachment__c = false ;                             
            }
            advicesToUpdate.add(a);   
        }
    }

    update advicesToUpdate;  // do DML outside of SOQL to prevent limits
}

 
This was selected as the best answer