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 

Test class for the trigger to create or update account

I have written a trigger on account to create or update contact. But there is something wrong with my trigger. When I am inserting the values, the same method is being called two times. 
What I am trying to do is:-
1) In the test class I am checking all the possible conditions for both the methods.

  • First I make three accounts, two with Create_Contact__c value as true.
  • For the account which has Create_Contact__c value as false, I want to check that there is no contact with same Accountid with the account in the list
This is my code for trigger
trigger CreateContactOnAccount on Account (before insert,after insert, before update, after update) {
    if(Trigger.isBefore && Trigger.isInsert){
        system.debug('"Testing "');
         List<Contact> addCon = new List<Contact>();
    	 for(Account acc :{
            if(acc.Create_Contact__c == true){
                Contact cons = new Contact(Lastname = acc.Name,
                	AccountId =,
            		Fax = acc.Fax,
            		Phone = acc.Phone,
            		MailingStreet = acc.BillingStreet,
            		MailingCity = acc.BillingCity,
            		MailingCountry = acc.BillingCountry,
        insert addCon;
    if(Trigger.isupdate && Trigger.isAfter){
        system.debug('Testing updatingaccount ');
        set<id> getaccountid =new set<id>(); //to get the accountid which is updating
        for(account account :{
   			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.Fax = account.Fax;
            	cons.Phone = account.Phone;
            	cons.MailingStreet = account.BillingStreet;
            	cons.MailingCity = account.BillingCity;
            	cons.MailingCountry = account.BillingCountry;
        	update addCon;
Code for test Class
public class CreateContactOnAccountTest {
	 @istest static void validateCreateContact(){
        Account acc = new Account();
		acc.Name='Test Account' ;
        acc.Phone= '7868754543';
		acc.Create_Contact__c = true;
        Account acc1 = new Account();
		acc1.Name='Test Account 1' ;
        acc1.Phone= '7868754543';
		acc1.Create_Contact__c = false;
        Account acc2 = new Account();
		acc2.Name='Test Account 2' ;
        acc2.Phone= '7868754543';
		acc2.Create_Contact__c = true;
        insert acc;
        insert acc1;
        insert acc2;
         list<contact> contactlist = [SELECT Id, Name,Phone, AccountId FROM contact ];
         System.assertEquals('Test Account', 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 =:];
        System.assertEquals('Test Account', contactlist[0].Name);
         System.assertEquals('767653656', contactlist[0].Fax);
        System.assertEquals('New Delhi', contactlist[0].MailingCity);

PriyaPriya (Salesforce Developers) 

Hi Sukriti,

Can you reexplain the error in the test code and line number as well.


Priya Ranjan