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
Avinash dhanke 24Avinash dhanke 24 

Trigger to Count number of Attachments on an Object in Salesforce

hello all,

i am tring to write trigger on attachment object to count no.of attachment  on an object and display count to on fileld that is on opportunity but i am not able to find count propr here below is my code
trigger CountAttachment on Attachment (after insert,after update,after delete,after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
  set<id> attids = new set<id>();
     
   if(Trigger.new<>null){
       for(Attachment c:Trigger.new){
           Opportunity l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }
           
   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }
   if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,Opportunity> testmap = new Map<id,Opportunity>([select id,Count_Attachment__c from Opportunity where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN:attids];
           
           for(Attachment at: a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }else if(parent.get(at.parentid) != null){
                   llist = new List<Attachment>();
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }
           
           for(Id i: attids){
               if(testmap.get(i) != null && parent.get(i) != null){
                  testmap.get(i).Count_Attachment__c = parent.get(i).size(); 
               
               }else if(testmap.get(i) != null && parent.get(i) == null){
                  testmap.get(i).Count_Attachment__c = 0; 
               }
           }
       
           update testmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

    
}
Steven NsubugaSteven Nsubuga
Try this
trigger CountAttachment on Attachment (after insert,after update,after delete,after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
  set<id> attids = new set<id>();
     
   if(Trigger.new<>null){
       for(Attachment c : Trigger.new){
           Opportunity l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }
           
   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }
   if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,Opportunity> testmap = new Map<id,Opportunity>([select id,Count_Attachment__c from Opportunity where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN :testmap.keyset()];
           
           for(Attachment at : a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid, llist);
               }else if(parent.get(at.parentid) != null){
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }
           
           for(Id i : parent.keyset()){
                testmap.get(i).Count_Attachment__c = parent.get(i).size();               
           }
           update testmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

    
}

 
Avinash dhanke 24Avinash dhanke 24
hello @Steven still not working ..my filed not populating count
 
Steven NsubugaSteven Nsubuga
hi @Avinash, I have tested it and it works. 
Make sure you are creating a File, not a Note. Also create a test class and see if it works.
Ensure that no validation rule, or process builder is affecting the trigger.

See my screenshot
User-added image
Avinash dhanke 24Avinash dhanke 24
@steven can you please check my trigger once again...and let mi know any issue that beause its not working
Steven NsubugaSteven Nsubuga
Use the developer console to debug the trigger. I see nothing wrong because as I have shown you, it works!
The difference between my version and yours is that mine is more efficient.

You used 
a = [select id,parentid from Attachment where parentid IN:attids];
while I used
a = [select id,parentid from Attachment where parentid IN :testmap.keyset()];

In my version I select attachments whose parentId is an Opportunity Id while yours selects all attachments including those whose parentId is not an Opportunity.

You use attids in the final for loop, while I use parent.keyset().  parent.keyset() only has Opportunity Ids while attids has Opportunity Ids and other sObject Ids which are parents to Attachments that have been edited.

Even so, your logic is ok. Use the debug log/developer console to see what is happening as the trigger runs. Check for errors. There is nothing more I can say.
Ankita TodkarAnkita Todkar

please give the test class also im also having prbl in code 
the count is not getting added in count field

Mr.HarryMr.Harry
trigger CountAttachment on Attachment (after insert,after update,after delete,after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
    set<Id> attIds = new set<Id>();
   
    if(Trigger.new <> null){
        for(Attachment at : Trigger.new){
            system.debug(at);
            if(at.ParentId != null)
                attIds.add(at.parentid);
        }
        
    }else if(Trigger.old != null){
        for(Attachment at:Trigger.old){
            if(at.ParentId<>null)      
                attIds.add(Trigger.oldMap.get(at.id).parentid);
        }
    }
    if(attIds.size()>0)
    {
        try
        {
            List<Attachment> a = new List<Attachment>();
            Map<id,Case> testmap = new Map<id,Case>([select id, Attachment_Count__c from Case where id IN: attIds]);
            a = [select id,parentid from Attachment where parentid IN :testmap.keyset()];
            
            for(Attachment att : a)
            {
                List<Attachment> llist = new List<Attachment>();
                if(parent.get(att.parentid) == null)
                {
                    llist = new List<Attachment>();
                    llist.add(att);
                    parent.put(att.parentid, llist);
                }
                else if(parent.get(att.parentid) != null)
                {
                    llist = parent.get(att.parentid);
                    llist.add(att);
                    parent.put(att.parentid,llist);
                }
            }
            
            for(Id i : parent.keyset())
            {
                testmap.get(i).Attachment_Count__c = parent.get(i).size();               
            }
            update testmap.values();
            System.Debug(testmap.values());
        }
        catch(Exception e){}
    }
       
}