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
Naveen KvpNaveen Kvp 

Test class for trigger??

Hi Everyone,

      Here i am trying to write test class for this trigger but i am getting only 42% of code covereage and DML operation bock of Update              consUpdate; is not covered. can you please help me to increse code covereage.
      
      here i am attach trigger and test class
      
 Thanks in advance.

Create_Contact trigger:
    
trigger Create_Contact on Account (After insert,After Update) {
  List<Contact> conlist=new List<Contact>();  
     if(Trigger.isInsert)
       {
       for(Account a: Trigger.New)
         {
         try
         {                            
         List<String> listTsks=a.Primary_Email__c.split('@');
         system.debug('@@@@@@@@@@@@@@@@@@'+listTsks);
         
          
             Contact c=new Contact();  
                c.AccountId=a.id;          
                c.LastName=listTsks[0];                             
                c.Phone= a.Phone;
                c.Email=a.Primary_Email__c;
                c.MobilePhone=a.Mobile__c;
                c.Description=a.Description;
             
                c.Address_Line_1__c=a.Address_Line_1__c;
                c.Address_Line_2__c=a.Address_Line_2__c;
                c.Address_Line_3__c=a.Address_Line_3__c;
                c.City__c=a.City__c;
                c.Country__c=a.Country__c;
                c.PIN_Code__c=a.PIN_Code__c; 
                
               conlist.add(c);                                            
               insert conlist;
               }
               
               catch(exception e)
               {
               }   
         } 
                       
       if(Trigger.isUpdate)
       {
       try
       {
       List<contact> consUpdate=New List<Contact>();
            List<Account> alist=[select id,name,Phone,Mobile__c,Primary_Email__c,Address_Line_2__c,Address_Line_1__c,Address_Line_3__c,Description,Country__c,PIN_Code__c,City__c from Account];
            List<id> ids=new List<id>();
          for(Account a:trigger.new)
           {
              ids.add(a.id);
           }
          List<contact> cons=[select id,name,AccountId,Phone,MobilePhone,Address_Line_2__c,Address_Line_1__c,Address_Line_3__c,Email,Country__c,PIN_Code__c,City__c,LastName from Contact where AccountId=:ids];
          for(Account a1:trigger.new)
          {
              for(Contact c:cons)
              {
                  if(a1.id==c.AccountId)
                  {
                   List<String> listTsks=a1.Primary_Email__c.split('@');
                c.AccountId=a1.id;                    
                c.LastName=listTsks[0];
                c.Phone= a1.Phone;
                c.Email=a1.Primary_Email__c;
                c.MobilePhone=a1.Mobile__c;
                c.Description=a1.Description;
            
                c.Address_Line_1__c=a1.Address_Line_1__c;                
                c.Address_Line_2__c=a1.Address_Line_2__c;
                c.Address_Line_3__c=a1.Address_Line_3__c;
                c.City__c=a1.City__c;
                c.Country__c=a1.Country__c;
                c.PIN_Code__c=a1.PIN_Code__c; 
               
                  consUpdate.add(c);
                  }
                 
              }
          }      
       if(consUpdate.size()>0)
       {
          Update consUpdate; 
       }
       }
       catch(exception e)
       {}
       }  
  }          
         
}

test class:

@isTest
  Public Class TestCreateContactTrigger
  {
    Static TestMethod Void createcontact()
    {
     
      Account a= new Account();
       a.Name='Test Acc';
       a.Phone='123';
       a.Primary_Email__c='test@email.com';
      
       insert a;
       update a;
     
     Contact c=new Contact();
      c.Salutation='Mr';
      c.LastName='a.Name';
      c.Email= a.Primary_Email__c;
      c.AccountId=a.id;
      
      insert c;
   
     
     Account aa = new Account(id=a.id,Name='Acc');
     Update aa;
     
     Contact cc = new Contact(id=c.id,LastName=aa.Name);
     Update cc;
     
      
    }
  }
Anoop yadavAnoop yadav
Hi,

Are you getting any error in the above test class?

Try with below code and let me know.
@isTest
Public Class TestCreateContactTrigger {
	Static TestMethod Void createcontact() {
     
		Account a= new Account();
		a.Name='Test Acc';
		a.Phone='123';
		a.Primary_Email__c='test@email.com';
		insert a;

		Contact c=new Contact();
		c.Salutation='Mr';
		c.LastName='a.Name';
		c.Email= a.Primary_Email__c;
		c.AccountId=a.id;
		insert c;      

		update a;
	}
}

 
Naveen KvpNaveen Kvp
Hi Anoop,
       
        I am not getting any error on obove test class,i am using your class same result. thanks for your response.if u have any ideas let me know.

Thanks,
Naveen
Anoop yadavAnoop yadav
Hi,
I just checkd your trigger.
In trigger your isUpdate condition is inside isInsert condition.

Replace your trigger with the below code and check.
trigger Create_Contact on Account (After insert,After Update) {
  List<Contact> conlist=new List<Contact>();  
     if(Trigger.isInsert)
       {
       for(Account a: Trigger.New)
         {
         try
         {                            
         List<String> listTsks=a.Primary_Email__c.split('@');
         system.debug('@@@@@@@@@@@@@@@@@@'+listTsks);
         
          
             Contact c=new Contact();  
                c.AccountId=a.id;          
                c.LastName=listTsks[0];                             
                c.Phone= a.Phone;
                c.Email=a.Primary_Email__c;
                c.MobilePhone=a.Mobile__c;
                c.Description=a.Description;
             
                c.Address_Line_1__c=a.Address_Line_1__c;
                c.Address_Line_2__c=a.Address_Line_2__c;
                c.Address_Line_3__c=a.Address_Line_3__c;
                c.City__c=a.City__c;
                c.Country__c=a.Country__c;
                c.PIN_Code__c=a.PIN_Code__c; 
                
               conlist.add(c);                                            
               insert conlist;
               }
               
               catch(exception e)
               {
               }   
         } 
		 }
                       
       if(Trigger.isUpdate)
       {
       try
       {
       List<contact> consUpdate=New List<Contact>();
            List<Account> alist=[select id,name,Phone,Mobile__c,Primary_Email__c,Address_Line_2__c,Address_Line_1__c,Address_Line_3__c,Description,Country__c,PIN_Code__c,City__c from Account];
            List<id> ids=new List<id>();
          for(Account a:trigger.new)
           {
              ids.add(a.id);
           }
          List<contact> cons=[select id,name,AccountId,Phone,MobilePhone,Address_Line_2__c,Address_Line_1__c,Address_Line_3__c,Email,Country__c,PIN_Code__c,City__c,LastName from Contact where AccountId=:ids];
          for(Account a1:trigger.new)
          {
              for(Contact c:cons)
              {
                  if(a1.id==c.AccountId)
                  {
                   List<String> listTsks=a1.Primary_Email__c.split('@');
                c.AccountId=a1.id;                    
                c.LastName=listTsks[0];
                c.Phone= a1.Phone;
                c.Email=a1.Primary_Email__c;
                c.MobilePhone=a1.Mobile__c;
                c.Description=a1.Description;
            
                c.Address_Line_1__c=a1.Address_Line_1__c;                
                c.Address_Line_2__c=a1.Address_Line_2__c;
                c.Address_Line_3__c=a1.Address_Line_3__c;
                c.City__c=a1.City__c;
                c.Country__c=a1.Country__c;
                c.PIN_Code__c=a1.PIN_Code__c; 
               
                  consUpdate.add(c);
                  }
                 
              }
          }      
       if(consUpdate.size()>0)
       {
          Update consUpdate; 
       }
       }
       catch(exception e)
       {}
       }  
          
         
}