+ Start a Discussion
aklkkaklkk 

how to write a Trigger on Attachments if the any attached file then status if approved else not approved on parent Fields ??

Hi, all

how to write a Trigger on Attachments if the any attached file then status if approved else not approved on parent Fields ??



thanks
aklkk
ArjunChowdaryArjunChowdary
Hi aklkk,

Please find the sample code for your requirment

trigger UpdateParentStatusIfAttachementIsTrue on Attachment (after insert) {
    
    //Collect all the Attachement object's Parent Id's in Set
    Set<Id> attIds = new Set<Id>();
    List<Case> listCases = new List<Case>();
    for(Attachment att : Trigger.New){
        attIds.add(att.ParentId);      
    }
    
    List<Case> lstCase = [SELECT Id, Attachment_Status__c FROM Case WHERE Id IN: attIds];
    
    if(lstCase.size()>0){
        for(Case c : lstCase){
            c.Attachment_Status__c = true;
            listCases.add(c);
        }
    }
    if(listCases.size()>0){
        update listCases;
    }

}