0

I come to ask for help to solve this problem I'm having with a flow. I have the custom Object "EnsaiosdeDM" that has a standard related list, what I need is that when a document is added to this list, a checkbox field that I created is checked, and when this list is empty, this field is unchecked. First I created a flow, which would do this, but it doesn't recognize the insertion of a document in the related list as an update, so it doesn't trigger the flow. Then I created a trigger:

trigger HasAttachment on ContentDocumentLink (after insert, before delete) {
String tempParentId;
Set<Id> setParentId = new Set<Id>();
List<Ensaios_de_DM__c> Essayolst = new List<Essaios_de_DM__c>();
    
    if(trigger.isBefore){
        for (ContentDocumentLink cdl : trigger.old ) {
            
            tempParentId = cdl.LinkedEntityId;
            
            if (tempParentId.left(3) =='a1f') {
                System.debug('Debug : found a1f');
                System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                setParentId.add(cdl.LinkedEntityId);
            }
        }
        Essayolst = [select Id , Has_Annex__c from Essays_de_DM__c where Id IN :setParentId];
        
        For(Ensaios_de_DM__c and : Essayolst)
        {
            e.Tem_Annexo__c = false;
        }
        
        update Essayolst;
        }
    else{
    
        for (ContentDocumentLink cdl : trigger.new ) {
            tempParentId = cdl.LinkedEntityId;
            
            if (tempParentId.left(3) =='a1f') {
                System.debug('Debug : found a1f');
                System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                setParentId.add(cdl.LinkedEntityId);
            }
        }
        Essayolst = [select Id , Has_Annex__c from Essays_de_DM__c where Id IN :setParentId];
        
        For(Ensaios_de_DM__c and : Essayolst)
        {
            e.Tem_Anexo__c = true;
        }
        
        update Essayolst;
}
}

It's working only to check the checkbox, but it doesn't uncheck when the related list is empty... does anyone know what I can do? If you know how to solve with flow I would also like to know lol

enter image description here

1 Answer 1

2

Before Delete triggers do not work for ContentDocumentLink. Use triggers on ContentDocument instead.

Please follow below links:

https://ideas.salesforce.com/s/idea/a0B8W00000GdWdyUAF/enable-trigger-delete-event-on-contentdocumentlink

https://help.salesforce.com/s/articleView?id=000312746&type=1

1
  • Thanks, I'll take a look at that material you sent. I'll let you know what I can develop @Sneh
    – Souza
    Commented Apr 25, 2022 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .