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
Sukriti SharmaSukriti Sharma 

Write a test class for creating and updating contact trigger on account

I wrote a trigger as followes:-
1) Create a contact 
  • If the checkbox (Create_Contact__c) while creating account is marked create contact wih same details.
  • If the checkbox is not marked create only account.
2) Update contact
  • If the user is updating the contact which has a related contact, update with the details added.
The trigger I wrote:-
trigger CreateContactOnAccount on Account (before insert,after insert, before update, after update) {
    if(Trigger.isBefore || Trigger.isInsert){
         List<Contact> addCon = new List<Contact>();
    	 for(Account acc : trigger.new){
            if(acc.Create_Contact__c == true){
                Contact cons = new Contact(Lastname = acc.Name,
                	AccountId = acc.id,
            		Fax = acc.Fax,
            		Phone = acc.Phone,
            		MailingStreet = acc.BillingStreet,
            		MailingCity = acc.BillingCity,
            		MailingCountry = acc.BillingCountry,
                    MailingPostalCode=acc.BillingPostalCode);
            	addCon.add(cons);
            }
        }
        insert addCon;
    }
    if(Trigger.isupdate && Trigger.isAfter){
        set<id> getaccountid =new set<id>(); //to get the accountid which is updating
        for(account account : trigger.new){
        	getaccountid.add(account.id);
   			list<contact> contactlist = [SELECT id, accountid FROM contact WHERE accountid =:getaccountid]; //gets the contact with same accountid
    		List<Contact> addCon = new List<Contact>();
    		for(Contact cons : contactlist){
        		Account acc = Trigger.newmap.get(cons.accountid);
            	cons.Lastname = account.Name;
        		//cons.AccountId = account.id;
            	cons.Fax = account.Fax;
            	cons.Phone = account.Phone;
            	cons.MailingStreet = account.BillingStreet;
            	cons.MailingCity = account.BillingCity;
            	cons.MailingCountry = account.BillingCountry;
            	addCon.add(cons);
       	 	}
        	update addCon;
    	}
    }
}
The Test class I have written so far. It is working for validateCreateContact() but not working for validateUpdateContact(). The error is logs is "List is out of bound". Please help me with the test class.
@isTest
public class CreateContactOnAccountTest {
	 @istest static void validateCreateContact(){
        
        Account acc = new Account();
		acc.Name='Test Account' ;
        acc.Phone= '7868754543';
		acc.Create_Contact__c = true;
	
		Test.startTest();
        insert acc;
        system.debug(acc.id);
        Test.stopTest();
        
         list<contact> contactlist = [SELECT Id, Name,Phone, AccountId FROM contact WHERE AccountId =: acc.id];
         System.debug(contactlist[0]);
         System.assertEquals('Test Account', contactlist[0].Name);
    } 
    @isTest static void validateUpdateContacts(){
		Account accnew = new Account();
		accnew.Name = 'Test Account 2';
        accnew.Phone = '76776537265';
       	insert accnew;
        
        accnew.Fax = '767653656';
        accnew.BillingCity = 'New Delhi';
        
        Test.startTest();
        update accnew;
        Test.stopTest();
        
        list<contact> contlist = [SELECT Id, Name,Phone, AccountId, Fax, MailingCity FROM contact WHERE AccountId =: accnew.id];
        System.debug(contlist[0]);
        System.assertEquals('767653656', contlist[0].Fax);
        System.assertEquals('New Delhi', contlist[0].MailingCity);
    }
}
Best Answer chosen by Sukriti Sharma
CharuDuttCharuDutt
Hii Sukirti
Try Below Test Class 100% Coverage
@isTest
public class CreateContactOnAccountTest {
    @istest static void validateCreateContact(){
        
        Account acc = new Account();
        acc.Name='Test Account' ;
        acc.Phone= '7868754543';
        acc.Create_Contact__c = true;
        insert acc;
        list<contact> contactlist = [SELECT Id, Name,Phone, AccountId FROM contact WHERE AccountId =: acc.id];
        System.debug(contactlist[0]);
        System.assertEquals('Test Accountxyz-{000001}', contactlist[0].Name);
        
    } 
    @isTest static void validateUpdateContacts(){
        Account acc = new Account();
        acc.Name='Test Account' ;
        acc.Phone= '7868754543';
        insert acc;
        acc.Create_Contact__c = true;
        acc.Fax = '767653656';
        acc.BillingCity = 'New Delhi';
        update acc;
        list<contact> contactlist = [SELECT Id, Name,Phone, AccountId, Fax, MailingCity FROM contact WHERE AccountId =: acc.id];
        System.debug(contactlist[0]);
        System.assertEquals('Test Account', contactlist[0].Name);
         System.assertEquals('767653656', contactlist[0].Fax);
        System.assertEquals('New Delhi', contactlist[0].MailingCity);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sukirti
Try Below Test Class 100% Coverage
@isTest
public class CreateContactOnAccountTest {
    @istest static void validateCreateContact(){
        
        Account acc = new Account();
        acc.Name='Test Account' ;
        acc.Phone= '7868754543';
        acc.Create_Contact__c = true;
        insert acc;
        list<contact> contactlist = [SELECT Id, Name,Phone, AccountId FROM contact WHERE AccountId =: acc.id];
        System.debug(contactlist[0]);
        System.assertEquals('Test Accountxyz-{000001}', contactlist[0].Name);
        
    } 
    @isTest static void validateUpdateContacts(){
        Account acc = new Account();
        acc.Name='Test Account' ;
        acc.Phone= '7868754543';
        insert acc;
        acc.Create_Contact__c = true;
        acc.Fax = '767653656';
        acc.BillingCity = 'New Delhi';
        update acc;
        list<contact> contactlist = [SELECT Id, Name,Phone, AccountId, Fax, MailingCity FROM contact WHERE AccountId =: acc.id];
        System.debug(contactlist[0]);
        System.assertEquals('Test Account', contactlist[0].Name);
         System.assertEquals('767653656', contactlist[0].Fax);
        System.assertEquals('New Delhi', contactlist[0].MailingCity);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
CharuDuttCharuDutt
Hii Sukirti please close your query by marking it as best answer so it also help others in future