0

I have setup a trigger to update a custom checkbox Attachment__c on opportunity on uploading or deletion of file as an attachment. This seems to work fine on the file upload but fails every time on deletion of the attached file. Please let me know if I am making any mistake. Thanks in advance for help!

Please find the code below:

trigger OppAttachmentTrigger2 on ContentDocument (after insert,after update) {
if(Trigger.isAfter && Trigger.isInsert ||Trigger.isAfter && Trigger.isUpdate){
    Set<Id> IdCollect = new Set<Id>();
    for(ContentDocument att:Trigger.New){
        if(att.ParentId!=null){
            IdCollect.add(att.ParentId);
        }
    }
    List<Opportunity> oppList = new List<Opportunity>();
    for(Opportunity oppy : [SELECT Id,Attachment__c FROM Opportunity where id=:IdCollect]){
        for(ContentDocument at:Trigger.New){
            if(at.ParentId!=null){
                oppy.Attachment__c = true;
                oppList.add(oppy);
            }    
        }   
    }
    update oppList; 
}

// After delete attachment///

if(Trigger.isBefore && Trigger.isDelete){
Set<Id> IdCollect = new Set<Id>();
for(ContentDocument att:Trigger.Old){
    system.debug('--att '+att);
    if(att.id!=null){
        IdCollect.add(att.Id);
    }
}
List<ContentDocumentLink> cdl = new List<ContentDocumentLink>();
cdl = [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN :IdCollect ];

system.debug('ContentDocumentLink ---- cdl '+cdl);
List<Opportunity> oppList = new List<Opportunity>();
for(Opportunity oppy : [SELECT Id,Attachment__c FROM Opportunity where id=:IdCollect]){   
    oppList.add(oppy);
   oppy.Attachment__c = false;
               }
               
update oppList;

} }

5
  • Hi @Sachin, Did you check if you are getting the parent id while inserting the new attachment by using the debug logs? Mar 29, 2022 at 11:47
  • Hi @SaiPraveenKakkirala I am new to coding and figuring out to make this code work. Mar 29, 2022 at 12:07
  • While inserting the attachment I see the parentid is null. Can you check if the code is working while inserting? Mar 29, 2022 at 12:10
  • Yes it works every time when the file is uploaded but fails every time when it is deleted. Mar 30, 2022 at 5:11
  • [Duplicate Post] Please refer to this link - salesforce.stackexchange.com/questions/328576/… Mar 30, 2022 at 5:48

2 Answers 2

1

When file is deleted, we are getting null parent id from your code. You will have to query contentdocumentlink from contentDocument and then get parent record id from there. Below is code I tried for before delete, in debug logs I am getting record id I linkedEntityId field. You can use it to update your field on record.

   if(Trigger.isBefore && Trigger.isDelete){
    Set<Id> IdCollect = new Set<Id>();
    for(ContentDocument att:Trigger.Old){
        system.debug('--att '+att);
        if(att.id!=null){
            IdCollect.add(att.Id);
        }
    }
    List<ContentDocumentLink> cdl = new List<ContentDocumentLink>();
    cdl = [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN :IdCollect ];

    system.debug('ContentDocumentLink ---- cdl '+cdl);
    List<Opportunity> oppList1 = new List<Opportunity>();
    for(Opportunity oppy : [SELECT Id FROM Opportunity where id=:IdCollect]){   
        oppList1.add(oppy);
    }     
    system.debug('oppList1 '+oppList1);
    update oppList1;
}

Hope this answers your question.

2
  • Hi @Muzammil. Thanks for your help due to some reason it is still not working on the deletion of file. For your reference I have updated my code above. Please let me know if I am doing this wrong. Apr 11, 2022 at 5:32
  • Can you add debug logs and check if you are getting this debug log -> ContentDocumentLink ---- Apr 11, 2022 at 9:47
-1

Hi Muzammil thank you for your help. However when i am using the code for my requirement it is still not working. Please let me know where am I going wrong.

 if(Trigger.isBefore && Trigger.isDelete){
Set<Id> IdCollect = new Set<Id>();
for(ContentDocument att:Trigger.Old){
    system.debug('--att '+att);
    if(att.id!=null){
        IdCollect.add(att.Id);
    }
}
List<ContentDocumentLink> cdl = new List<ContentDocumentLink>();
cdl = [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN :IdCollect ];
system.debug('ContentDocumentLink ---- cdl '+cdl);
List<Opportunity> oppList = new List<Opportunity>();
for(Opportunity oppy : [SELECT Id,Attachment__c FROM Opportunity where id=:IdCollect]){   
    oppList.add(oppy);
   oppy.Attachment__c = false;
               }
               
update oppList;

}

1
  • Sorry, but this doesn't answer the question you asked. Instead of posting this as an answer, please update the question! Mar 30, 2022 at 6:46

You must log in to answer this question.

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