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
srikanth cheera 13srikanth cheera 13 

write a test class for This

trigger ContactNamesOnAccount on Contact (after update, after insert) {
Set<id> accIdList = new Set<id>();
 for(Cotact con : Trigger.new){
accIdList.add(con.accountid);
}
List<Account> accUpdateList = new List<Account>();
List<String> names = new List<String>;
for(Account acc : [Select id, Contact_Names__c, (Select LastName From Contacts) From Account Where Id In : accIdList])
{
for(Contact con : acc.contacts){
if(con.LastName != null){
 /* add name to list */
names.add(con.LastName);
}
}
/* update name separating ', ' */
acc.Contact_Names__c = String.join(names, ', '); 
accUpdateList.add(acc);
/* clear list to add new account contact names */
names.clear();
}
update accUpdateList;
}
Raj VakatiRaj Vakati
Use this
@isTest
public class ContactNamesOnAccountTest{

    static testMethod void testex2() {
    
		Test.startTest();
         Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
		
		Contact cont = new Contact();
		cont.FirstName='Test';
		cont.LastName='Test';
		cont.Accountid= testAccount.id;
		insert cont;
		
		
		Contact cont1 = new Contact();
		cont1.FirstName='Test';
		cont1.LastName='Test';
		cont1.Accountid= testAccount.id;
		insert cont1;
		
		
       Test.stopTest();  
     }
   } 
 }