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
vijay kumar kvijay kumar k 

show the number of attachments in account custom field

hi please help me to write test class for below trigger or post the test class

trigger countattachments on attachment(after insert, after delete,after undelete) {
    Map<Id, List<attachment>> Amap = new Map<Id, List<attachment>>();
    Set<Id> AcctIds = new Set<Id>();   
    List<Account> AcctList = new List<Account>();
    List<attachment> alist = new List<attachment>();
   
    if(trigger.isInsert||trigger.isundelete ) {
        for(attachment a : trigger.New) {
            if(String.isNotBlank(a.parentid)){
                AcctIds.add(a.parentId); 
            }  
        } 
    }
   
    if(trigger.isDelete) {
        for(attachment a1 : trigger.Old) {
            AcctIds.add(a1.parentId);    
        } 
    }          
   
    if(AcctIds.size() > 0){
        alist = [SELECT Id, parentId FROM attachment WHERE parentId IN : AcctIds];
       
        for(attachment a2 : alist) {
            if(!Amap.containsKey(a2.parentId)){
                Amap.put(a2.parentid, new List<attachment>());
            }
            Amap.get(a2.parentid).add(a2);     
        }                          
      
           
        AcctList = [SELECT Number_of_attachments__c FROM Account WHERE Id IN : AcctIds];
        for(Account Acc : AcctList) {
            List<attachment> atList = new List<attachment>();
            atList = Amap.get(Acc.Id);
            Acc.Number_of_attachments__c = atList.size();
        }   
       
      
        update AcctList;   
    }

}

thank you
vijay
 
Raj VakatiRaj Vakati
@isTest
public class TestAttachment { 

	//Method
	static testMethod void InsertAttachment() {

	Account a = new Account();
	a.Name='Test';
	insert b;

	Attachment attach = new Attachment();

	attach.Name='Unit Test Attachment';
	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
	attach.body=bodyBlob;
	attach.parentId=b.id;
	insert attach;

	List<Attachment> attachments=[select id, name from Attachment where parent.id=:b.id];
	System.assertEquals(1, attachments.size());

	 attach = [SELECT Id, name from Attachment where parent.id=:b.id];
	delete attach;  

		

}
}

 
vijay kumar kvijay kumar k
Thank 
But my trigger shows error at delete the last attachment , so please help me. 
Raj VakatiRaj Vakati
try this code
 
@isTest
public class TestAttachment { 
    
    //Method
    static testMethod void InsertAttachment() {
        
        Account a = new Account();
        a.Name='Test';
        insert a;
        
        Attachment attach = new Attachment();
        
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=a.id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id];
        System.assertEquals(1, attachments.size());
        
        
        
    }
}

 
Raj VakatiRaj Vakati
This is 100 % 
 
@isTest
public class TestAttachment { 
    
    //Method
    static testMethod void InsertAttachment() {
        
        Account a = new Account();
        a.Name='Test';
        insert a;
        
        Attachment attach = new Attachment();
        
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=a.id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id];
        System.assertEquals(1, attachments.size());
        try{
            attach = [SELECT Id, name from Attachment where parent.id=:a.id];
            delete attach;  
            
        }catch(Exception e){
            
        }
        
        
    }
}