0

can someone help me out? I have a checkbox field and when the user inserts a file this field is 'true' through a trigger, how can I make it 'false' if the user deletes the files? I really appreciate anyone who can help. My first attempt was to create a flow that analyzed if the files existed, but it doesn't seem possible to do that anymore...

trigger HasAttachment on ContentDocumentLink (after insert) {
String tempParentId;
    Set<Id> setParentId = new Set<Id>();
    List<Ensaios_de_DM__c> Ensaiolst = new List<Ensaios_de_DM__c>();
    
 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);
            }
        }
    Ensaiolst = [select Id , Tem_Anexo__c from Ensaios_de_DM__c where Id IN :setParentId];
     
     For(Ensaios_de_DM__c e : Ensaiolst)
     {
        e.Tem_Anexo__c = true;
     }

     update Ensaiolst;
}
2
  • As you mentioned, you need to set the flag as 'false' if the user deletes the file. So, I believe if you add 'after delete' event in your trigger and add the logic for making the flag as false in 'after delete' event this might work. Apr 14, 2022 at 19:14
  • I'm new to salesforce, what would that logic look like? I barely understand this code I posted lol @tandonprateek
    – Souza
    Apr 14, 2022 at 19:24

1 Answer 1

1

I have modified your code little bit, used your trigger and a class to differentiate the logic between the trigger events.

HasAttachment.trigger

    trigger HasAttachment on ContentDocumentLink (after insert, before delete) {

    ContentDocumentLinkTriggerHelper handler = new ContentDocumentLinkTriggerHelper();
        if(Trigger.isDelete && Trigger.isBefore){
            handler.OnBeforeDelete(Trigger.oldMap);
        }
    
    if(Trigger.isInsert && Trigger.isAfter){
            handler.OnAfterInsert(Trigger.newMap);
        }


}

ContentDocumentLinkTriggerHelper.cls

public class ContentDocumentLinkTriggerHelper {
    public void onAfterInsert(Map<ID, ContentDocumentLink> newContentDocumentLinkMap){
        String tempParentId;
            Set<Id> setParentId = new Set<Id>();
            List<Ensaios_de_DM__c> ensaiolst = new List<Ensaios_de_DM__c>();
    
        for (ContentDocumentLink cdl : newContentDocumentLinkMap.values() ) {
                    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);
                    }
            }
            ensaiolst = [select Id , Tem_Anexo__c from Ensaios_de_DM__c where Id IN :setParentId];
     
            For(Ensaios_de_DM__c e : ensaiolst){
                e.Tem_Anexo__c = true;
            }

            update Ensaiolst;
    }

    public void onBeforeDelete(Map<ID, ContentDocumentLink> oldContentDocumentLinkMap){
        String tempParentId;
            Set<Id> setParentId = new Set<Id>();
        List<Ensaios_de_DM__c> ensaiolst = new List<Ensaios_de_DM__c>();
    
        for (ContentDocumentLink cdl : oldContentDocumentLinkMap.values() ) {
            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);
                    }
            }
            ensaiolst = [select Id , Tem_Anexo__c from Ensaios_de_DM__c where Id IN :setParentId];
     
            For(Ensaios_de_DM__c e : ensaiolst){
                e.Tem_Anexo__c = false;
            }

            update Ensaiolst;
    }
}

I have not tested this code, but might give you some idea.

You must log in to answer this question.

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