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
Anshuman ParhiAnshuman Parhi 

Build solution to show count of number of Attachment (Classic +Lightening) on record detail page (Create custom filed to store count). Count should get updated on the basis of delete /Insert.

Any suggestion for this.....thanku in advance
ShivankurShivankur (Salesforce Developers) 
Hi Anshuman,

Here is an example of trigger on Attachment object which could count the files for a provided Task object record:
trigger CountAttachment on Attachment (after insert, after update, after delete, after undelete) {
    // Contains the IDs of all the parent tasks
    Set<Id> parentTaskIdSet = new Set<id>();

    if (trigger.new != null)
    {
        for (Attachment a: trigger.new)
        {
            parentTaskIdSet.add(a.parentId);
        }
    }
    
    if (trigger.old != null)
    {
        for (Attachment a: trigger.old)
        {
            parentTaskIdSet.add(a.parentId);
        }
    }    
    
    // List of tasks that needs to be updated
    List<Task> parentTaskList = [SELECT id, (SELECT id FROM Attachments) FROM Task WHERE id in: parentTaskIdSet];
    
    for (Task t: parentTaskList)
    {
        t.NumberOfAttachments__c = t.Attachments.size();
    }
    
    update parentTaskList;
}

You can customize similar logic for the target object which you want to implement.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Anshuman ParhiAnshuman Parhi
Hi Shivankur 
Could you give me some realtime example....i am not able to find attachment object where i can create a custom NumberofAttachment__c field.
CharuDuttCharuDutt
Hii AyushMan
Try Below Code
As Attachment File Can Be Uploaded To Any Object 
Total Count Of Attachment Of Account Record Is Set In Field Made In Account Object "Total_Attachments__c" So You Can Get How Many Attchments A Particular Record Holds


trigger AttachmentCount on Attachment (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Attachment con : Trigger.new){
            setAccIds.Add(con.parentId);
			}
		}
    }
     system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Attachment con : Trigger.new){ 

               	setAccIds.add(con.parentId);
                           	}
			}        
        }
    
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Attachment con : Trigger.old) { 
            if(con.parentId != null){
            setAccIds.add(con.parentId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Total_Attachments__c,(SELECT id FROM Attachments) from Account where Id in : setAccIds]){
        acc.Total_Attachments__c= acc.Attachments.size();
       acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}


Test Class:
@isTest
private class TestAccountFileUpload {

      static testMethod void testAttachments()
    {
        list<Attachment> lstAttupdate = new list<Attachment>();
        Account acc=new Account(Name='Acme Inc');
        insert acc;
        
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
        
        list<Attachment> lstAtt = [Select id,Name,ParentId From Attachment Where ParentId=:Acc.Id];
        for(Attachment Att:lstatt){
            Att.Name = 'Test';
            lstAttupdate.add(Att);
        }
        update lstAtt;
        delete lstAtt;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:acc.id];
        System.assertEquals(1, attachments.size());
    }
}
Please Mark It As Best Answer If It Helps
Thank You!